MEMCACHED_FETCH_EXECUTE(3)

πŸ’– MEMCACHED_FETCH_EXECUTE πŸ’–

okay so hi bestie!! you found the page for memcached_fetch_execute β€” it lives in the libmemcached-awesome manual, section 3, and it's literally the whole vibe of "how do i get my data back out of the server" ✨ grab a snack, this one's a family reunion of functions 🫢


🌸 NAME 🌸

memcached_fetch_execute β€” retrieving data from the server πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <libmemcached/memcached.h>

and like, compile + link with:

-libmemcached

wait no, it's -lmemcached β€” typo era, we don't do that here πŸ™ˆ anyway. the whole squad of signatures:

memcached_result_st *memcached_fetch_result(memcached_st *ptr,
memcached_result_st *result, memcached_return_t *error)

char *memcached_get(memcached_st *ptr, const char *key, size_t
key_length, size_t *value_length, uint32_t *flags, memcached_return_t
*error)

memcached_return_t memcached_mget(memcached_st *ptr, const char *const
*keys, const size_t *key_length, size_t number_of_keys)

char *memcached_get_by_key(memcached_st *ptr, const char *group_key,
size_t group_key_length, const char *key, size_t key_length, size_t
*value_length, uint32_t *flags, memcached_return_t *error)

memcached_return_t memcached_mget_by_key(memcached_st *ptr, const char
*group_key, size_t group_key_length, const char *const *keys, const
size_t *key_length, size_t number_of_keys)

memcached_return_t memcached_fetch_execute(memcached_st *ptr,
memcached_execute_fn *callback, void *context, uint32_t
number_of_callbacks)

memcached_return_t memcached_mget_execute(memcached_st *ptr, const char
*const *keys, const size_t *key_length, size_t number_of_keys,
memcached_execute_fn *callback, void *context, uint32_t
number_of_callbacks)

memcached_return_t memcached_mget_execute_by_key(memcached_st *ptr,
const char *group_key, size_t group_key_length, const char *const *keys,
const size_t *key_length, size_t number_of_keys, memcached_execute_fn
*callback, void *context, uint32_t number_of_callbacks)

typedef memcached_return_t (*memcached_execute_fn)(const memcached_st
*ptr, memcached_result_st *result, void *context)

πŸ¦‹ DESCRIPTION πŸ¦‹

✨ the getters ✨

memcached_get() is your girl for grabbing one single value off the server. you pass in a key and its length to fetch the object. you also gotta hand over three pointer variables so she can tell you the state of the returned object, and yes all three are mandatory, no skipping πŸ’…

  • a uint32_t pointer β€” it holds whatever flags you stored alongside the value
  • a size_t pointer β€” gets filled in with the size of the object
  • a memcached_return_t pointer β€” holds any error

on success you get the object back πŸŽ‰ on failure you get NULL 😭 and any object returned by memcached_get() has to be released by the caller application β€” that's you, bestie, that's your job, don't ghost it.

✨ the multigetters ✨

memcached_mget() is the "select a bunch of keys at once" icon. for multiple key operations it is always faster to use this function β€” like, genuinely always, no cap. and she always works asynchronously, periodt. ⚑

to actually retrieve data after a successful run of memcached_mget(), you're gonna need to call memcached_fetch_result(). keep calling it until it hands you a NULL (that's the signal for "no more values, we're done here"). and if you need to bail out in the middle of a memcached_mget() call, you can execute a memcached_quit() β€” though this is not required. do it for the drama if you want 🧚

✨ fetch_result, the icon ✨

memcached_fetch_result() fetches an individual value from the server. memcached_mget() must always be called before you use this method β€” like, that's the prerequisite, that's the lore.

you must pass in a key and its length to fetch the object, and again you supply three pointer variables which will give you the state of the returned object:

  • a uint32_t pointer β€” containing whatever flags you stored with the value
  • a size_t pointer β€” filled with the size of the object
  • a memcached_return_t pointer β€” holding any error

the object comes back on success, NULL comes back on failure. MEMCACHED_END is returned by the *error value when all objects that have been found have been returned. and the final value upon MEMCACHED_END is null. 🫠

also β€” and this is the part people sleep on β€” memcached_fetch_result() is used to return a memcached_result_st structure from a memcached server. that result object is forward compatible with changes to the server πŸ‘‘ for more info pls refer to the memcached_result_st help. and if you don't pass one in? she will dynamically allocate a result structure for you. so generous, so main character. ✨

✨ fetch_execute, the callback queen ✨

memcached_fetch_execute() is a callback function for result sets. instead of handing the results back to you for processing, she passes each of the result sets to the list of functions you provide. here's what each function call receives:

  • a memcached_st that can be cloned for use in the called function β€” but it can not be used directly, ok? clone it, don't touch it πŸ’€
  • a result set, which does not need to be freed πŸ™Œ
  • a "context" β€” which is just a pointer to a memory reference you supply the calling function

currently only one value is being passed to each function call. in the future there will be an option to allow this to be an array. future tea β˜•

✨ mget_execute ✨

memcached_mget_execute() and memcached_mget_execute_by_key() are similar to memcached_mget(), but they may trigger the supplied callbacks with result sets while sending out the queries. why does that matter?? because if you try to perform a really large multiget with memcached_mget() you may encounter a deadlock in the OS kernel 😱 (it will fail to write data to the socket because the input buffer is full). memcached_mget_execute() solves this problem by processing some of the results before continuing to send out requests. slay

⚠️ pls note that this function is only available in the binary protocol. no binary protocol, no party.

✨ the by_key duo ✨

memcached_get_by_key() and memcached_mget_by_key() behave in a similar nature as memcached_get() and memcached_mget(). the difference? they take a master key that is used for determining which server an object was stored on, if key partitioning was used for storage. that's it, that's the whole plot twist πŸ”₯

✨ the UDP warning, listen up ✨

πŸ’€ all of the above functions are not tested when MEMCACHED_BEHAVIOR_USE_UDP has been set. executing any of these functions with this behavior on will result in MEMCACHED_NOT_SUPPORTED being returned β€” or, for those functions which do not return a memcached_return_t, the error function parameter will be set to MEMCACHED_NOT_SUPPORTED. so like. don't. πŸ™ˆ

πŸ’œ RETURN VALUE πŸ’œ

  • all objects retrieved via memcached_get() or memcached_get_by_key() must be freed with free(3). free your friends, bestie 🫢
  • memcached_get() will return NULL on error. you must look at the value of error to determine what the actual error was. she won't just tell you out loud, you gotta read the room.
  • memcached_fetch_execute() returns MEMCACHED_SUCCESS if all keys were successful. MEMCACHED_NOTFOUND will be returned if no keys at all were found. 😭
  • memcached_fetch_result() sets error to MEMCACHED_END upon successful conclusion. MEMCACHED_NOTFOUND will be returned if no keys at all were found. (yes, same energy. consistency is iconic.)

⭐ SEE ALSO ⭐

memcached(1) libmemcached(3) memcached_strerror(3)


and that's the page!! πŸŽ€ this whole thing is from version 1.1, dated Jul 24, 2025 β€” so like, recent, fresh, and fully in her era. go forth and fetch responsibly, bestie πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from