MEMCACHED_I...ITH_INITIAL(3)

πŸŽ€ MEMCACHED_I...ITH_INITIAL πŸŽ€

ok so bestie, this page lives in libmemcached-awesome, section 3 of the manual (yes, that's the library-functions section, we're fancy like that πŸ’…). Pull up a chair, grab a strawberry πŸ“, and let's talk about counting things in memcached.


✨ NAME ✨

memcached_increment_with_initial β€” Incrementing and Decrementing Values πŸ”’


🌸 SYNOPSIS 🌸

First things first, you gotta pull in the header and link the library:

#include <libmemcached/memcached.h>
       Compile and link with -lmemcached

Then here's the whole squad of functions, no cap:

memcached_return_t memcached_increment(memcached_st *ptr, const char
*key, size_t key_length, uint32_t offset, uint64_t *value)

memcached_return_t memcached_decrement(memcached_st *ptr, const char
*key, size_t key_length, uint32_t offset, uint64_t *value)

memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
const char *key, size_t key_length, uint64_t offset, uint64_t initial,
time_t expiration, uint64_t *value)

memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
const char *key, size_t key_length, uint64_t offset, uint64_t initial,
time_t expiration, uint64_t *value)

memcached_return_t memcached_increment_by_key(memcached_st *ptr, const
char *group_key, size_t group_key_length, const char *key, size_t
key_length, uint32_t offset, uint64_t *value)

memcached_return_t memcached_decrement_by_key(memcached_st *ptr, const
char *group_key, size_t group_key_length, const char *key, size_t
key_length, uint32_t offset, uint64_t *value)

memcached_return_t memcached_increment_with_initial_by_key(memcached_st
*ptr, const char *group_key, size_t group_key_length, const char *key,
size_t key_length, uint64_t offset, uint64_t initial, time_t expiration,
uint64_t *value)

memcached_return_t memcached_decrement_with_initial_by_key(memcached_st
*ptr, const char *group_key, size_t group_key_length, const char *key,
size_t key_length, uint64_t offset, uint64_t initial, time_t expiration,
uint64_t *value)

🫢 Parameters

ok so here's what every single one of these lil guys means, don't skip, i know it's a lot 😭

  • ptr -- pointer to an initialized memcached_st struct
  • group_key -- key namespace
  • group_key_length -- length of the key namespace without any terminating zero
  • key -- the key
  • key_length -- length of the key without any terminating zero
  • offset -- offset to increment/decrement
  • initial -- initial value if key does not exist and expiration is not MEMCACHED_EXPIRATION_NOT_ADD
  • expiration -- expiration as a unix timestamp or as relative expiration time in seconds
  • value -- the resulting value after initialization/increment/decrement

⭐ Returns

memcached_return_t indicating success. that's it, that's the tea 🍡


πŸ’– DESCRIPTION πŸ’–

so memcached(1) servers have this ability to increment and decrement keys, and honestly? iconic. overflow and underflow are not detected, bestie, so keep that in mind πŸ‘€. What this gives you is the power to use memcached to generate shared sequences of values. it's giving ✨shared counter energy✨.

memcached_increment() takes a key and key_length and increments the value by the offset passed to it. The value is then returned via the uint32_t value pointer you pass to it. slay.

memcached_decrement() takes a key and key_length and decrements the value by the offset passed to it. The value is then returned via the uint32_t value pointer you pass to it. the way it just... goes down πŸ’€

memcached_increment_with_initial() takes a key and key_length and increments the value by the offset passed to it. Now here's where it gets interesting: if the object specified by key does not exist, one of two things may happen:

  • If the expiration value is MEMCACHED_EXPIRATION_NOT_ADD, the operation will fail. no key, no counter, we move on 😀
  • For all other expiration values, the operation will succeed by seeding the value for that key with a initial value to expire with the provided expiration time.

The flags will be set to zero. The value is then returned via the uint32_t value pointer you pass to it. memcached_increment_with_initial is only available when using the binary protocol. iykyk 🧚

memcached_decrement_with_initial() takes a key and key_length and decrements the value by the offset passed to it. Same deal when the object specified by key doesn't exist β€” one of two things may happen:

  • If the expiration value is MEMCACHED_EXPIRATION_NOT_ADD, the operation will fail. periodt.
  • For all other expiration values, the operation will succeed by seeding the value for that key with a initial value to expire with the provided expiration time.

The flags will be set to zero. The value is then returned via the uint32_t value pointer you pass to it. memcached_decrement_with_initial() is only available when using the binary protocol. yeah, binary only, no cap 🚫

memcached_increment_by_key(), memcached_decrement_by_key(), memcached_increment_with_initial_by_key(), and memcached_decrement_with_initial_by_key() are master key equivalents of the above. they said "let's do the same thing but with a group_key" and honestly? obsessed. πŸ‘‘


πŸ”₯ RETURN VALUE πŸ”₯

A value of type memcached_return_t is returned. On success that value will be MEMCACHED_SUCCESS. Use memcached_strerror() to translate this value to a printable string. so if something's weird, that's your translator bestie πŸ¦‹


🌈 SEE ALSO 🌈

memcached(1) libmemcached(3) memcached_strerror(3)


and that's the whole page, bestie!! πŸ’–βœ¨ this was libmemcached-awesome, version 1.1, dated Jul 24, 2025 β€” go forth and increment responsibly πŸŽ€πŸ«ΆπŸ’…

πŸ“¦ where this page comes from