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 initializedmemcached_ststructgroup_key-- key namespacegroup_key_length-- length of the key namespace without any terminating zerokey-- the keykey_length-- length of the key without any terminating zerooffset-- offset to increment/decrementinitial-- initial value if key does not exist and expiration is notMEMCACHED_EXPIRATION_NOT_ADDexpiration-- expiration as a unix timestamp or as relative expiration time in secondsvalue-- 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
expirationvalue isMEMCACHED_EXPIRATION_NOT_ADD, the operation will fail. no key, no counter, we move on π€ - For all other
expirationvalues, the operation will succeed by seeding the value for that key with ainitialvalue to expire with the providedexpirationtime.
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
expirationvalue isMEMCACHED_EXPIRATION_NOT_ADD, the operation will fail. periodt. - For all other
expirationvalues, the operation will succeed by seeding the value for that key with ainitialvalue to expire with the providedexpirationtime.
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
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms