MEMCACHED_CAS(3)
π MEMCACHED_CAS β bestie's guide to Atomic Compare and Swap π
okay so this is memcached_cas from the libmemcached-awesome manual, section 3 (that's the library-functions girlies, not the commands one). grab a snack, we're doing atomic writes π
β¨ NAME β¨
memcached_cas</pη΄«θ²> β Atomic Compare and Swap. that's it, that's the name. the whole vibe is "only write if nothing changed underneath you." no cap.
πΈ SYNOPSIS πΈ
first things first, you gotta include the header, and like, actually link the library or none of this works:
#include <libmemcached/memcached.h>
and then Compile and link with -lmemcached β do not forget this, bestie, I am begging π
here are the two functions:
memcached_return_t memcached_cas(memcached_st *ptr, const char *key,
size_t key_length, const char *value, size_t value_length, time_t
expiration, uint32_t flags, uint64_t cas)
memcached_return_t memcached_cas_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, uint64_t cas)
π Parameters
ptrβ pointer to an initializedmemcached_ststruct. it's gotta be initialized, we do not do uninitialized structs in this house πgroup_keyβ the key namespace. it's giving "which server do I belong to" energy.group_key_lengthβ the length of thegroup_key, and without any trailing zero please and thank you.keyβ the key you're checking πkey_lengthβ length of thekey, again without any trailing zero.valueβ the value you wanna set.value_lengthβ length of thevalue, you already know the rule: no trailing zero.expirationβ expiration time, either as a unix timestamp or a relative time in seconds. pick your fighter β¨flagsβ 16 bit flags. sixteen. bits. not more.casβ the cas value to compare. this is THE whole point of the function, bestie, don't sleep on it.
π«Ά Returns
a memcached_return_t indicating success. slay.
π¦ DESCRIPTION π¦
okay so here's the tea. memcached_cas() overwrites the data in the server as long as the cas value is still the same in the server. that's the "atomic" part, that's the whole personality. if somebody else changed it in between, the cas won't match and it won't clobber their work. queen behavior honestly π
and how do you even get that cas value? you call memcached_result_cas() on a memcached_result_st structure. that's where it lives, go get it π§
memcached_cas_by_key() does basically the same thing as the non-key methods β same energy, same rules β except it uses the group_key parameter to map objects to particular servers. so it's the "I have opinions about which server this lives on" version.
now, the important part, so listen up π’
memcached_cas() is tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. However β and this is a big however β when you use these operations with that behavior turned on, there are limits to the size of the payload being sent to the server. and why, you ask? because the Memcached Server does not allow multi-datagram requests, and the current server implementation sets a datagram size to 1400 bytes. π
and because of protocol overhead, the actual limit of the user supplied data is less than 1400 bytes, and it depends on the protocol in use as well as the operation being executed. so it's giving "it depends," which, I know, but that's just the truth of it.
here's the breakdown, no cap:
- with the binary protocol, that's
MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, the size of the key, value, flags and expiry combined may not exceed 1368 bytes. - 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, andvalue. - for cas ASCII operations, there are at least 1318 bytes available to split among the
key,key_prefixandvalue.
- for non-cas ASCII set operations, there are at least 1335 bytes available to split among the
π¨ and if the total size of the command, including overhead, exceeds 1400 bytes, a MEMCACHED_WRITE_FAILURE will be returned. so likeβ¦ budget your bytes, bestie. π
β RETURN VALUE β
all methods return a value of type memcached_return_t. on success the value will be MEMCACHED_SUCCESS. and if you want it in human-readable form, call memcached_strerror() to translate this value to a printable string. it's giving translator bestie π
π SEE ALSO π
go read these next, they're all connected, iykyk:
memcached(1)libmemcached(3)memcached_strerror(3)memcached_set(3)memcached_append(3)memcached_add(3)memcached_prepend(3)memcached_replace(3)
and that's the whole page, babes β¨π this one's from libmemcached-awesome, version 1.1, dated Jul 24, 2025. go forth and compare-and-swap responsibly, mwah π«Άπ
π¦ where this page comes from
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms