MEMCACHED_ADD(3)

πŸ’– MEMCACHED_ADD β€” storing and replacing data (libmemcached-awesome girlies rise) πŸ’–

okay so bestie, pull up a chair, because we're about to talk about the three sisters of memcached storage: memcached_set(), memcached_add(), and memcached_replace(). this is MEMCACHED_ADD(3) from libmemcached-awesome and she's lowkey iconic. no cap. πŸ¦‹


✨ SYNOPSIS ✨

first things first, you gotta include the header and link the lib, obviously:

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

and here are the six function besties in all their glory β€” copy them exactly, don't be cute with it:

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 literally all just here to store info on the server. that's the whole vibe. πŸ’…

every one of these methods takes a key, plus its length, to store the object. and heads up: 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. you must supply both a value and a length. optionally you store the object. (yes, the original says this twice, and honestly? relatable. 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 β€” and bestie, that 16 byte thing is meant to be used as a bitmap. "flags" is a 4byte space that's stored right alongside the main value. lots of sub libraries make use of this field, so in most cases users should avoid making use of it. iykyk. πŸ™ˆ


πŸŽ€ the three sisters, explained πŸŽ€

  • memcached_set() β€” writes 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 non-blocking mode, this function will always return true unless a network error occurs. main character energy fr fr. πŸ‘‘
  • memcached_replace() β€” replaces an object on the server. if the object is not found on the server, an error occurs. like, she needs the object to already exist. that's her 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. she's the "no duplicates allowed" girlie. πŸ’€

🌈 the by_key variants 🌈

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 like, same energy, but with ✨routing✨.


πŸ”₯ performance tea πŸ”₯

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


🫢 the UDP situation (important, bestie) 🫢

all of the above functions are tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however, when you use 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, watch your byte budget, queen. πŸ’€

⭐ 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. slay. ✨

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 doing its job. πŸŽ€


πŸ‘‘ SEE ALSO πŸ‘‘

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


and that's the whole page, bestie!! πŸ’– this was libmemcached-awesome, version 1.1, dated Jul 24, 2025 β€” go forth and store your data responsibly, mwah 🫢✨🌸

πŸ“¦ where this page comes from