MEMCACHED_REPLACE_BY_KEY(3)

πŸ’– MEMCACHED_REPLACE_BY_KEY πŸ’–

okay bestie, so this is libmemcached-awesome's page for memcached_replace_by_key, and it's giving "how do I put stuff on the server and how do I swap it out" energy. sit down, grab a boba, we're doing this πŸ§‹βœ¨


✨ NAME ✨

memcached_replace_by_key β€” storing and replacing data, the whole vibe πŸŽ€


🌸 SYNOPSIS 🌸

first things first, you gotta bring the header in:

#include <libmemcached/memcached.h>

and you compile and link with -lmemcached. no cap.

here are all the little guys in the family (copy these exact, okay? don't be out here renaming things πŸ’…):

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() β€” all three of them β€” are the ones you use to store information on the server. every single one takes a key plus its length to store the object. and heads up: keys are 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. you must supply both a value and a length. optionally you store the object. (yes we said that twice, the original said it twice, we keep it twice, that's the rule πŸ™ˆ). also keys are currently limited to 250 characters by the memcached(1) server. 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's meant to be used as a bitmap). "flags" is a 4byte space that gets stored alongside of the main value. a bunch of sub libraries make use of this field, so in most cases users should lowkey avoid making use of it 🫣

memcached_set() will write an object to the server. if the object already exists it will overwrite what's 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 isn't found on the server, an error occurs. that's it, that's the whole thing 😭

memcached_add() adds an object to the server. if the object is found on the server, an error occurs. otherwise the value is stored. periodt ✨

memcached_set_by_key(), memcached_add_by_key(), and memcached_replace_by_key() β€” these three 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. it's giving "same energy, but with routing" 🌈

if you're looking for performance, memcached_set() with non-blocking IO is the fastest way to store data on the server. no competition, it ate πŸ”₯

all of the above functions are tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however β€” and this is the part where you pay attention bestie β€” when you use these operations with that behavior turned 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. πŸ’€

fr fr, do the math before you send. the server will not be nice about it, it'll just hand you that failure.

πŸ¦‹ RETURN VALUE πŸ¦‹

all methods return a value of type memcached_return_t. icon behavior.

on success the value will be MEMCACHED_SUCCESS. use memcached_strerror() to translate this value to a printable string, iykyk πŸŽ€

for memcached_replace() and memcached_add(), MEMCACHED_NOTSTORED is a legitimate error in the case of a collision. so if you see it, don't panic, it's just the vibe check saying "someone got there first" 😭


⭐ SEE ALSO ⭐

memcached(1) libmemcached(3) memcached_strerror(3) memcached_prepend(3) memcached_append(3) memcached_cas(3)

go read those too, they're all part of the lore πŸ‘‘


and that's the whole page bestie πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

this was libmemcached-awesome version 1.1, dated Jul 24, 2025 β€” main character manpage energy, no cap. go store something cute πŸŒΈπŸ’œ

πŸ“¦ where this page comes from