MEMCACHED_SET(3)

πŸ’– MEMCACHED_SET πŸ’–

okay bestie, gather round, because we're talking about libmemcached-awesome's memcached_set(3) page and honestly? it's giving storage. this is the one about putting data on your memcached server, replacing it, and adding it, and there are some lil rules you absolutely cannot skip, so let's go 🫢


✨ NAME ✨

memcached_set β€” Storing and Replacing Data. that's it, that's the whole vibe. storing. replacing. the two genders of cache πŸ’…


✨ SYNOPSIS ✨

first up, the include situation. you're gonna want this at the top of your file:

#include <libmemcached/memcached.h>

and then compile and link with -lmemcached β€” do NOT forget that, i'm serious, the linker will simply Not Know Her 😭

here's all six of the function signatures, verbatim, no notes:

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)

look at them. six lil icons. all returning memcached_return_t because consistency is key (pun fully intended, i will not be apologising) πŸ’œ


✨ DESCRIPTION ✨

so. memcached_set(), memcached_add(), and memcached_replace() are the trio that put information on the server. all of them take a key, plus the length of that key, so the object can be stored.

⚠️ key length warning, bestie: 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. also keys are currently limited to 250 characters by the memcached(1) server. yes it says it twice. no i'm not deleting it. the manpage said it, so i say it. 🌸

you must supply both a value and a length. optionally, you store the object. optionally you may test an expiration time for the object, and a 16 byte value (it is meant to be used as a bitmap). and "flags" is a 4byte space that gets stored right alongside the main value. many sub libraries make use of this field, so in most cases users should avoid making use of it. like, hands off, it's not yours, it's the sub libraries' πŸ™ˆ

okay now the actual behaviours:

  • memcached_set() will write an object to the server. if an 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, honestly.
  • memcached_replace() replaces an object on the server. if the object is not found on the server, an error occurs. no object, no replace, periodt.
  • memcached_add() adds an object to the server. if the object is found on the server, an error occurs; otherwise the value is stored. it's the inverse of replace and it knows it.

and now the group-key girlies:

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 bit. that's the tea β˜•

if you're looking for performance, memcached_set() with non-blocking IO is the fastest way to store data on the server. main character energy fr fr πŸ”₯

πŸ¦‹ the UDP situation πŸ¦‹

all of the above functions are tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however β€” and this is a big however, bestie, listen up β€” 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.

here's the breakdown, numbers exact, do not @ me:

  • 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.
πŸ’€ and if the total size of the command, including overhead, exceeds 1400 bytes, a MEMCACHED_WRITE_FAILURE will be returned. that's it. that's the whole message. it just fails. respect the datagram, she has boundaries.

✨ RETURN VALUE ✨

all methods return a value of type memcached_return_t. every single one. no exceptions. 🌈

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 a personality, bestie, turn them into words.

for memcached_replace() and memcached_add(), MEMCACHED_NOTSTORED is a legitimate error in the case of a collision. so don't panic when you see it, it's not a bug, it's the whole system working exactly as designed 😌


✨ SEE ALSO ✨

go read these too, they're all iconic in their own right:


okay that's the whole page, you ate that πŸ’– this was libmemcached-awesome version 1.1, dated Jul 24, 2025 β€” go forth and store your data responsibly, ily, mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from