MEMCACHED_MGET_EXECUTE_BY_KEY(3)

πŸ’– memcached_mget_execute_by_key β€” the libmemcached-awesome era πŸ’–

okay bestie, so this one lives in the libmemcached-awesome manual, section 3 (that's the "library functions" section, iykyk). Pull up a chair, grab a boba, and let's talk about pulling data out of the server. πŸ§‹βœ¨


✨ NAME ✨

memcached_mget_execute_by_key β€” retrieving data from the server 🫢


🌸 SYNOPSIS 🌸

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

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)

like… look at all those signatures. it's giving ✨choices✨. no cap. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

the single-fetch girlies: memcached_get()

memcached_get() fetches one individual value from the server. You gotta pass in a key and its key_length to grab the object. And you must supply three pointer variables so you can actually see the state of the thing you got back:

  • a uint32_t pointer to hold whatever flags you stored alongside the value
  • a size_t pointer that gets filled with the size of the object
  • a memcached_return_t pointer to hold any error

On success you get the object back; on failure you get NULL. And listen β€” any object returned by memcached_get() must be released by the caller application. that's on you, bestie. free it. 🫢

the multiget icon: memcached_mget()

memcached_mget() selects multiple keys at once. For multiple key operations it is always faster to use this function β€” literally always. And it always works asynchronously. periodt. ⭐

To retrieve data after a successful execution of memcached_mget(), you need to call memcached_fetch_result(). Keep calling it until it returns a NULL (that means no more values, we're done here πŸŽ€). And if you need to bail in the middle of a memcached_mget() call, you can execute a memcached_quit() β€” though that's not required.

memcached_fetch_result(), the sequel

memcached_fetch_result() fetches an individual value from the server. memcached_mget() must always be called before using this method. You must pass in a key and its length to fetch the object, and you must supply three pointer variables that give you the state of the returned object:

  • a uint32_t pointer to contain whatever flags you stored with the value
  • a size_t pointer which will be filled with the size of the object
  • a memcached_return_t pointer to hold any error

The object is returned upon success, and NULL is returned on failure. MEMCACHED_END is returned by the *error value when all objects that have been found have been returned. The final value upon MEMCACHED_END is null. πŸ‘‘

memcached_fetch_result() is also used to return a memcached_result_st structure from a memcached server. The result object is forward compatible with changes to the server. For more info, go check the memcached_result_st help. And here's the cute part: this function will dynamically allocate a result structure for you if you don't pass one in. it's got your back. 🧚

memcached_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, it passes each of the result sets to the list of functions you provide. To each of those functions it passes:

  • a memcached_st that can be cloned for use in the called function (it can not be used directly)
  • a result set which does not need to be freed
  • a "context" β€” just a pointer to a memory reference you supply to 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. (manifesting that for us ✨)

memcached_mget_execute() and memcached_mget_execute_by_key()

These two are similar to memcached_mget(), but they may trigger the supplied callbacks with result sets while sending out the queries. Here's why that slays: 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. πŸ’€

Please note that this function is only available in the binary protocol. πŸ™ˆ

the _by_key girlies

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 (group_key) that is used for determining which server an object was stored on, if key partitioning was used for storage. πŸ’œ

⚠️ the UDP warning, do not skip this one ⚠️

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.

fr fr, don't do it. it's giving undefined and untested. 🚫

πŸ’… RETURN VALUE πŸ’…

All objects retrieved via memcached_get() or memcached_get_by_key() must be freed with free(3). (yes, again. it's that important.)

memcached_get() will return NULL on error. You must look at the value of error to determine what the actual error was. πŸ‘€

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.


🌈 SEE ALSO 🌈

go read those too bestie, they're iconic πŸ’–


and that's the whole page!! this was libmemcached-awesome version 1.1, dated Jul 24, 2025 β€” shoutout to the authors and the copyright holders for this absolute banger of a manpage. mwah. πŸ˜˜πŸŽ€

now go free your memory and slay βœ¨πŸ¦‹πŸ’–

πŸ“¦ where this page comes from