LIBMEMCACHED_EXAMPLES(3)

πŸ’– LIBMEMCACHED_EXAMPLES πŸ’–

okay bestie, welcome to the libmemcached_examples page from the libmemcached-awesome docs β€” it's giving "here's how you actually use this thing" energy, and honestly? iconic. ✨

🌸 NAME 🌸

libmemcached_examples β€” the libmemcached documentation, and also... examples for libmemcached 🫢

πŸ“– DESCRIPTION πŸ“–

so if you want the full examples, the real ones live in tests/*.c in the main distribution. like, they're literally right there. those files are always up to date (no cap) and they get used for every single test run of the library. so yeah, that's the main character source of truth πŸ’…


πŸ”Œ CONNECTING TO SERVERS πŸ”Œ

const char *config_string =
  "--SERVER=host10.example.com "
  "--SERVER=host11.example.com "
  "--SERVER=host10.example.com";
memcached_st *memc= memcached(config_string, strlen(config_string);
{
  // ...
}
 memcached_free(memc);

so what's happening here: you make a memcached_st object with three servers by making use of memcached_create(). that's it, that's the vibe. πŸ‘‘


πŸŽ€ CREATING A POOL OF SERVERS πŸŽ€

const char *config_string =
  "--SERVER=host10.example.com "
  "--SERVER=host11.example.com "
  "--SERVER=host10.example.com";

memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));

memcached_return_t rc;

memcached_st *memc= memcached_pool_pop(pool, false, &rc);

// .... do work

/*
  Release the memc_ptr that was pulled from the pool
*/
memcached_pool_push(pool, memc);

/*
  Destroy the pool.
*/
memcached_pool_destroy(pool);

in the code above you create a memcached_pool_st object with three servers by making use of memcached_pool(). pool party fr fr 🌊

and when you call memcached_pool_destroy(), all memory associated with the pool gets released. like, everything. clean slate, so satisfying 😌


✨ ADDING A VALUE TO THE SERVER ✨

char *key= "foo";
char *value= "value";
time_t expires = 0;
uint32_t flags = 0;

memcached_return_t rc = memcached_set(memc,
                                     key, strlen(key),
                                     value, value_length,
                                     expires, flags);

if (rc != MEMCACHED_SUCCESS)
{
    // handle failure
}
bestie, listen: it is best practice to always look at the return value of any operation. always. no skipping, no "it'll be fine", no nada. check the rc. periodt. πŸ’…

πŸ¦‹ FETCHING MULTIPLE VALUES πŸ¦‹

memcached_return_t rc;
char *keys[]= {"fudge", "son", "food"};
size_t key_length[]= {5, 3, 4};
unsigned int x;
uint32_t flags;

char return_key[MEMCACHED_MAX_KEY];
size_t return_key_length;
char *return_value;
size_t return_value_length;

rc= memcached_mget(memc, keys, key_length, 3);

x= 0;
while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
                                 &return_value_length, &flags, &rc)))
{
    free(return_value);
    x++;
}

notice how you freed the values returned from memcached_fetch()? yeah, do that. the define MEMCACHED_MAX_KEY is provided for usage, so you've got that going for you 🌈


πŸ‘€ SEE ALSO πŸ‘€


and that's the whole page, bestie 🫢 this was libmemcached-awesome version 1.1, dated Jul 24, 2025 β€” go forth and cache responsibly, mwah πŸ’–πŸ’…βœ¨

πŸ“¦ where this page comes from