MEMCACHED_ADD_BY_KEY(3)

πŸ’Ύ MEMCACHED_ADD_BY_KEY β€” bestie edition πŸ’Ύ

okay so. this is a page from libmemcached-awesome, and the star of the show is memcached_add_by_key(3). it's giving "how do i put stuff on the server and how do i replace it" energy, and honestly? iconic. let's get into it 🫢


✨ NAME ✨

memcached_add_by_key β€” Storing and Replacing Data πŸŽ€


✨ SYNOPSIS ✨

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

memcached_return_t memcached_set(memcached_st *ptr, const char *key,
size_t key_length, const char *value, size_t value_length, time_t
expiration, uint32_t flags)

memcached_return_t memcached_add(memcached_st *ptr, const char *key,
size_t key_length, const char *value, size_t value_length, time_t
expiration, uint32_t flags)

memcached_return_t memcached_replace(memcached_st *ptr, const char *key,
size_t key_length, const char *value, size_t value_length, time_t
expiration, uint32_t flags)

memcached_return_t memcached_set_by_key(memcached_st *ptr, const char
*group_key, size_t group_key_length, const char *key, size_t key_length,
const char *value, size_t value_length, time_t expiration, uint32_t
flags)

memcached_return_t memcached_add_by_key(memcached_st *ptr, const char
*group_key, size_t group_key_length, const char *key, size_t key_length,
const char *value, size_t value_length, time_t expiration, uint32_t
flags)

memcached_return_t memcached_replace_by_key(memcached_st *ptr, const
char *group_key, size_t group_key_length, const char *key, size_t
key_length, const char *value, size_t value_length, time_t expiration,
uint32_t flags)

like look at that lineup. six whole functions, all just vibing, all ready to help you shove data somewhere. πŸ’…


🌸 DESCRIPTION 🌸

so memcached_set(), memcached_add(), and memcached_replace() β€” the whole trio β€” are what you use to store information on the server. all of them want a key, plus that key's length, so they know what they're storing the object under.

real talk about keys: keys are currently limited to 250 characters when you're using either a version of memcached(1) that's 1.4 or below, or when you're using the text protocol. also β€” and this is not a drill β€” you must supply both a value and a length. keys are currently limited to 250 characters by the memcached(1) server (yes, it says it twice, the manpage is just being thorough bestie πŸ™ˆ). you must supply both a value and a length. optionally you may test an expiration time for the object and a 16 byte value (it is meant to be used as a bitmap). "flags" is a 4byte space that is stored alongside of the main value. a bunch of sub libraries make use of this field, so in most cases users should avoid making use of it. 🚫

memcached_set() will write an object to the server. if an object already exists it will overwrite what is in the server. if the object does not exist it will be written. and if you're using the non-blocking mode, this function will always return true unless a network error occurs. 😌

memcached_replace() replaces an object on the server. if the object is not found on the server, an error occurs. it's giving "if you're not there i literally cannot help you". πŸ’€

memcached_add() adds an object to the server. if the object is found on the server, an error occurs β€” otherwise the value is stored. no duplicates in this house, periodt. πŸ”₯

memcached_set_by_key(), memcached_add_by_key(), and memcached_replace_by_key() all behave in a similar method as the non key methods. the difference is that they use their group_key parameter to map objects to particular servers. πŸ—ΊοΈ that's the whole twist, and it's lowkey genius.

if you are looking for performance, memcached_set() with non-blocking IO is the fastest way to store data on the server. ⚑ not me telling you the cheat code.

all of the above functions are tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however β€” and this is a big however β€” when using these operations with this behavior on, there are limits to the size of the payload being sent to the server. the reason for these limits is that the Memcached Server does not allow multi-datagram requests and the current server implementation sets a datagram size to 1400 bytes. because of protocol overhead, the actual limit of the user supplied data is less than 1400 bytes and depends on the protocol in use, as well as the operation being executed. 😭

so like, the receipts:

  • when running with the binary protocol, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, the size of the key, value, flags and expiry combined may not exceed 1368 bytes.
  • when running with the ASCII protocol, the exact limit fluctuates depending on which function is being executed and whether the function is a cas operation or not.
  • for non-cas ASCII set operations, there are at least 1335 bytes available to split among the key, key_prefix, and value.
  • for cas ASCII operations there are at least 1318 bytes available to split among the key, key_prefix and value.
if the total size of the command, including overhead, exceeds 1400 bytes, a MEMCACHED_WRITE_FAILURE will be returned. πŸ’€ don't do it. or do, but know what's coming.

πŸ¦‹ RETURN VALUE πŸ¦‹

all methods return a value of type memcached_return_t. πŸ“¬

on success the value will be MEMCACHED_SUCCESS. use memcached_strerror() to translate this value to a printable string. it's the little translator that could, we love her. πŸ’–

for memcached_replace() and memcached_add(), MEMCACHED_NOTSTORED is a legitimate error in the case of a collision. yes bestie, it's allowed to happen. it's not a bug, it's a whole situation. πŸ™ˆ


🌈 SEE ALSO 🌈

go read those too, they're all part of the same little friend group. 🫢


okay that's the whole page!! this was libmemcached-awesome version 1.1, dated Jul 24, 2025, and it's been an absolute honor explaining storing-and-replacing data to you. mwah πŸ’…βœ¨ slay, go store something responsibly, and remember: 1400 bytes is the limit, no cap. πŸŽ€πŸ’–

πŸ“¦ where this page comes from