MEMCACHED_CAS_BY_KEY(3)
β¨ MEMCACHED_CAS_BY_KEY β¨
okay bestie, so this is a page from the libmemcached-awesome manual β it's memcached_cas_by_key(3) and it's literally the "change it, but only if nobody else touched it first" of the memcached world π
no cap.
π NAME π
memcached_cas_by_key β storing and replacing data, but make it compare-and-swap π
πΈ SYNOPSIS πΈ
first things first, you gotta pull in the header and link the lib:
#include <libmemcached/memcached.h>
Compile and link with -lmemcached
and here are the two functions, looking all iconic:
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 π
okay here's the whole squad of arguments, one by one, no skips:
ptr-- pointer to an initializedmemcached_ststructgroup_key-- key namespacegroup_key_length-- length of thegroup_keywithout any trailing zerokey-- the key to checkkey_length-- the length of thekeywithout any trailing zerovalue-- the value to setvalue_length-- the length of thevaluewithout any trailing zeroexpiration-- expiration time as unix timestamp or relative time in secondsflags-- 16 bit flagscas-- the cas value to compare
π¦ Returns π¦
a memcached_return_t indicating success π« (we love a return type that just tells you how it went)
π DESCRIPTION π
so like. memcached_cas() overwrites data in the server as long as the cas value is still the same in the server. that's the whole plot twist β if somebody else already changed it, your write does NOT just bulldoze through. you can grab the cas value of a result by calling memcached_result_cas() on a memcached_result_st structure π«Ά
memcached_cas_by_key() behaves in a similar way as the non key methods. the difference is that it uses the group_key parameter to map objects to particular servers πΊοΈ (iykyk β it's how you pin stuff to the server you actually want)
memcached_cas() is tested with the MEMCACHED_BEHAVIOR_USE_UDP behavior enabled. however, when you're using these operations with this behavior turned on, there are limits to the size of the payload being sent to the server π and 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.
and because protocol overhead is a thing, 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. let's break it down:
- when running with the binary protocol,
MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, the size of the key, value, flags and expiry combined may not exceed1368bytes. - 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
1335bytes available to split among thekey,key_prefix, andvalue - for cas ASCII operations there are at least
1318bytes available to split among thekey,key_prefixandvalue
- for non-cas ASCII set operations, there are at least
π and if the total size of the command, including overhead, exceeds1400bytes, aMEMCACHED_WRITE_FAILUREwill be returned. so like... don't do that.
β 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 giving cryptic, and we want readable π«Ά
π SEE ALSO π
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 page bestie π this was 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