Memcac...ed_get(3pm)

✨ NAME ✨

bestie, this is memcached_get, memcached_mget, memcached_fetch, memcached_mget_execute, memcached_mget_execute_by_key β€” aka the whole "get a value" friend group πŸ’– (the manual page for this crowd lives in the Memcac...ed_get(3pm) section of the Perl docs, and honestly? iconic.)

πŸ“š LIBRARY πŸ“š

C Client Library for memcached (libmemcached, -lmemcached)


🌸 SYNOPSIS 🌸

okay so here's the whole squad in their natural habitat, no cap, copy-paste ready:

#include <memcached.h>

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 *master_key, size_t master_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 *master_key, size_t master_key_length,
                         const char * const *keys,
                         const size_t *key_length,
                         size_t number_of_keys);

char *
  memcached_fetch (memcached_st *ptr,
                   char *key, size_t *key_length,
                   size_t *value_length,
                   uint32_t *flags,
                   memcached_return_t *error);

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 *master_key,
                                 size_t master_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);

πŸ’… DESCRIPTION πŸ’…

πŸ“ memcached_get()

so memcached_get() is your girl for grabbing one individual value off the server. you pass in a key and its length to fetch the object ✨. you also have to hand over three pointer variables that'll tell you the state of the returned object:

  • a uint32_t pointer that'll hold whatever flags you stored with the value
  • a size_t pointer that gets filled with the size of of the object πŸ‘‘
  • a memcached_return_t pointer to hold any error

the object comes back to you on success, and NULL comes back on failure β€” that's just how it is, fr fr. and listen: any object returned by memcached_get() must be released by the caller application. that's on you bestie, don't leak it πŸ™ˆ


πŸ¦‹ memcached_mget()

memcached_mget() is the one you call when you wanna select multiple keys at once. for multiple key operations it is always faster to use this function β€” periodt πŸ’…. it always works asynchronously, so memcached_fetch() is what you use next to retrieve any keys found. no error is given on keys that are not found (they're just not found, it's giving nothing 🫢).

you must call either memcached_fetch() or memcached_fetch_result() after a successful call to memcached_mget(). keep calling those functions until they return NULL (aka no more values). and if you need to quit in the middle of a memcached_get() call, execute a memcached_quit(). after you do that, you can issue new queries against the server 🌈.


🌸 memcached_fetch()

memcached_fetch() is used to fetch an individual value from the server. memcached_mget() must always be called before using this method β€” no skipping steps, that's the rule βœ‹.

you pass in a key and its length to fetch the object. you supply three pointer variables giving 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 size of of the object
  • a memcached_return_t pointer to hold any error

the object is returned upon success and NULL is returned on failure. MEMCACHD_END is returned by the *error value when all objects that have been found are returned. the final value upon MEMCACHED_END is null. values returned by memcached_fetch() musted be free'ed by the caller.

⚠️ heads up bestie: memcached_fetch() will be DEPRECATED in the near future. memcached_fetch_result() should be used instead. we're moving on, it's a glow-up πŸ’…

πŸŽ€ memcached_fetch_result()

memcached_fetch_result() is used to return a memcached_result_st(3) structure from a memcached server. the result object is forward compatible with changes to the server ✨. for more info please refer to the memcached_result_st(3) help. this function will dynamically allocate a result structure for you if you do not pass one to the function β€” so generous of it 🫢


🧚 memcached_fetch_execute()

memcached_fetch_execute() is a callback function for result sets. instead of returning the results to you for processing, it passes each of the result sets to the list of functions you provide πŸ”₯. it passes to the function:

  • a memcached_st that can be cloned for use in the called function (it can not be used directly β€” clone it, don't touch the original πŸ’€)
  • 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 πŸ‘€


πŸ’œ 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 the tea: if you try to perform a really large multiget with memcached_mget() you may encounter a deadlock in the OS kernel 😭 β€” and the reason is we 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 sending out requests. slay πŸ’…

🌟 please note that this function is only available in the binary protocol.

🌈 memcached_get_by_key() and memcached_mget_by_key()

these 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 if key partitioning was used for storage πŸ”‘βœ¨


🚫 one big group-wide warning

all of the above functions are not supported when the 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.

like. don't do it bestie. UDP said no πŸ™…β™€οΈ


⭐ RETURN ⭐

all objects returned must be freed by the calling application β€” every single one, no exceptions, that's the vibe check βœ…

memcached_get() and memcached_fetch() will return NULL on error. you must look at the value of error to determine what the actual error was πŸ”

MEMCACHED_KEY_TOO_BIG is set to error whenever memcached_fetch() was used and the key was set larger then MEMCACHED_MAX_KEY, which was the largest key allowed for the original memcached ascii server. (yes, "larger then" β€” we're quoting the source, don't @ me 😭)


🏠 HOME 🏠

to find out more information please check: <https://launchpad.net/libmemcached>


πŸ‘‘ AUTHOR πŸ‘‘

Brian Aker, <brian@tangent.org>


πŸ’– SEE ALSO πŸ’–

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


and that's the whole page bestie, we ate and left no crumbs πŸ“βœ¨ this was perl v5.42.3, dated 2026-08-19, straight from the Memcac...ed_get(3pm) files β€” go forth and get your values, ily πŸ’œπŸ¦‹

πŸ“¦ where this page comes from