MEMCACHED_SERVER_LIST(3)

๐Ÿ’– MEMCACHED_SERVER_LIST ๐Ÿ’–

okay so bestie, welcome to memcached_server_list from libmemcached-awesome (that's the MEMCACHED_SERVER_LIST(3) page, third section, we're in library land โœจ). this is the whole vibe of managing your little roster of memcached servers. pull up a chair, grab a boba, let's go ๐Ÿง‹


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <libmemcached/memcached.h>
๐Ÿ“Ž Compile and link with -lmemcached โ€” don't forget this or nothing works and you'll be sad, no cap.
uint32_t memcached_server_count(memcached_st *ptr)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct

Returns

  • number of configured servers ๐Ÿ”ข
memcached_return_t memcached_server_add(memcached_st *ptr, const char *hostname, in_port_t port)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • hostname โ€” hostname or IP address of the TCP server to add
  • port โ€” port of the TCP server

Returns

  • memcached_return_t indicating success โœจ
memcached_return_t memcached_server_add_udp(memcached_st *ptr, const char *hostname, in_port_t port)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • hostname โ€” hostname or IP address of the UDP server to add
  • port โ€” port of the UDP server

Returns

  • memcached_return_t indicating success โœจ
memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr, const char *socket)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • socket โ€” path to the UNIX socket of the server to add

Returns

  • memcached_return_t indicating success โœจ
memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_st *list)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • list โ€” pre-configured list of servers to push

Returns

  • memcached_return_t indicating success โœจ
const memcached_instance_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return_t *error)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • key โ€” key to hash and lookup a server
  • key_length โ€” length of key without any terminating zero
  • error โ€” pointer to memcached_return_t indicating success

Returns

  • the server instance to be used for storing/retrieving key ๐Ÿ”‘
const memcached_instance_st *memcached_server_get_last_disconnect(const memcached_st *ptr)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct

Returns

  • the instance of the last server for which there was a connection problem ๐Ÿ’€
memcached_return_t memcached_server_cursor(const memcached_st *ptr, const memcached_server_fn *callback, void *context, uint32_t number_of_callbacks)

Parameters

  • ptr โ€” pointer to initialized memcached_st struct
  • callback โ€” list of memcached_server_fn to be called for each server instance
  • context โ€” pointer to user supplied context for the callback
  • number_of_callbacks โ€” number of callbacks supplied

Returns

  • memcached_return_t indicating success โœจ
typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, const memcached_instance_st *server, void *context)

Param ptr

  • pointer to the memcached_st struct

Param server

  • pointer to memcached_instance_st

Param context

  • pointer to user supplied context

Returns

  • memcached_return_t indicating success โœจ

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

so here's the tea, bestie: libmemcached does its operations on a list of hosts, and the order of those hosts is literally what decides routing to keys. yes, order matters, it's giving very main character energy. there are functions provided to add keys to memcached_st structures. and if you wanna mess with lists of servers themselves, go look at memcached_server_st ๐Ÿซถ

memcached_server_count() hands you a count of the current number of servers being used by a memcached_st structure. simple, iconic, no notes.

memcached_server_add() pushes a single TCP server into the memcached_st structure, and it gets placed at the end (back of the line, respectfully). duplicate servers are allowed โ€” so like, nobody checks for duplication, it's not that kind of party. โš ๏ธ but heads up: if you execute this function with the MEMCACHED_BEHAVIOR_USE_UDP behavior set, you get a MEMCACHED_INVALID_HOST_PROTOCOL. pick a lane, babe.

memcached_server_add_udp() pushes a single UDP server into the memcached_st structure. also placed at the end. also allows duplicates, also checks nothing. โš ๏ธ and the flip side: executing this one without setting the MEMCACHED_BEHAVIOR_USE_UDP behavior results in a MEMCACHED_INVALID_HOST_PROTOCOL. the two functions are literally mirror opposites, it's giving twins ๐Ÿ’…

memcached_server_add_unix_socket() pushes a single UNIX socket into the memcached_st structure. yep, it goes at the end. yep, duplicates allowed, yep, not checked. ๐Ÿ“ one thing though: the length of the filename must be one character less than MEMCACHED_MAX_HOST_LENGTH. count your characters, queen.

memcached_server_push() pushes an array of memcached_server_st into the memcached_st structure. servers go at the end, duplicates allowed, duplication not checked โ€” you know the drill by now. ๐Ÿ”ฅ and important: a copy is made of the structure, so the list you provided (and any operations you do on that list afterwards) are not saved. it's a copy, not a link, periodt.

memcached_server_by_key() lets you hand over a key and get back the server that would be used for assignment. it's giving "who's gonna handle this one" ๐Ÿ”ฎ

memcached_server_get_last_disconnect() returns a pointer to the last server that had a connection problem. ๐Ÿฉบ and listen, this does not mean that particular server is currently dead โ€” but if the library is reporting that some server is, then the returned server is a very good candidate. like, prime suspect energy.

memcached_server_cursor() takes a memcached_st and loops through the list of hosts currently in the cursor, calling the list of callback functions you provided. you can optionally pass in a value via context, which gets handed to each callback function. ๐Ÿ›‘ an error return from any callback will terminate the loop. also memcached_server_cursor() is passed the original caller memcached_st in its current state.


โญ RETURN VALUE โญ

Varies, see particular functions. (yes that's the whole thing, it's a choose-your-own-adventure situation, iykyk ๐Ÿ™ˆ)


๐ŸŽ€ SEE ALSO ๐ŸŽ€


and that's the whole page bestie!! ๐Ÿ’œ this was libmemcached-awesome version 1.1, dated Jul 24, 2025, and honestly? she ate. go forth and configure your servers, you've got this ๐Ÿ‘‘โœจ๐ŸŒธ

๐Ÿ“ฆ where this page comes from