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
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- fedora 43: libmemcached-awesome-devel 1.1.4-7.fc43 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms