MEMCACHED_SET_BY_KEY(3)

πŸ’– MEMCACHED_SET_BY_KEY πŸ’–

okay bestie, welcome to the libmemcached-awesome manual, page MEMCACHED_SET_BY_KEY. yes it's a long name, yes it's iconic, we move. this is the page about Storing and Replacing Data and honestly? it's giving essential. πŸŽ€


✨ NAME ✨

memcached_set_by_key β€” Storing and Replacing Data πŸ“


🌸 SYNOPSIS 🌸

so first, you gotta pull in the header, like:

#include <libmemcached/memcached.h>

and then, bestie, compile and link with -lmemcached. no cap, it won't link without it. πŸ’…

here's the whole friend group of functions, exactly as they are:

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)

πŸ¦‹ DESCRIPTION πŸ¦‹

so. memcached_set(), memcached_add(), and memcached_replace() are all here to store information on the server. every single one of them takes a key, plus the length of that key, so the server knows what it's dealing with. πŸ’œ

about keys: they're currently limited to 250 characters when you're using a version of memcached(1) that's 1.4 or below, or when you're using the text protocol. and yeah, you must supply both a value and a length β€” no vibes-only mode, sorry. 😭

optionally you store the object. and just to say it again because the manpage said it again: keys are currently limited to 250 characters by the memcached(1) server, and you must supply both a value and a length. also 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. like, it's not your lane, bestie, leave it. πŸ™ˆ

now the actual behaviour, function by function:

  • 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. slay. ✨
  • memcached_replace() replaces an object on the server. if the object is not found on the server, an error occurs. it's giving strict. πŸ’€
  • memcached_add() adds an object to the server. if the object is found on the server, an error occurs β€” otherwise the value is stored. 🫢
  • 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. so you can be like "this one goes over there" and the group key handles it. πŸ’«

if you are looking for performance, memcached_set() with non-blocking IO is the fastest way to store data on the server. periodt. πŸ”₯

all of the above functions are tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however β€” and this is the part where you lean in, bestie β€” 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. due to 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.

  • 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. so like, don't overshare to the server. it will not eat it up. πŸ’€

πŸ‘‘ 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, because raw return codes are not the moment. ✨

for memcached_replace() and memcached_add(), MEMCACHED_NOTSTORED is a legitimate error in the case of a collision. it's not a bug, it's just the rules, iykyk. πŸŽ€


🌈 SEE ALSO 🌈


okay that's the whole page bestie, you ate. 🫢 this was libmemcached-awesome version 1.1, dated Jul 24, 2025 β€” go store some data, you're the main character of this server. πŸ’…πŸŒΈβœ¨

πŸ“¦ where this page comes from