4 Examples for libmemcached
9 For full examples, test cases are found in tests/\*.c in the main
10 distribution. These are always up to date, and are used for each test run of
18 const char *config_string =
19 "--SERVER=host10.example.com "
20 "--SERVER=host11.example.com "
21 "--SERVER=host10.example.com";
22 memcached_st *memc= memcached(config_string, strlen(config_string);
28 In the above code you create a :type:`memcached_st` object with three server
29 by making use of :func:`memcached_create`.
31 Creating a pool of servers
32 --------------------------
36 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com";
38 memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
40 memcached_return_t rc;
42 memcached_st *memc= memcached_pool_pop(pool, false, &rc);
47 Release the memc_ptr that was pulled from the pool
49 memcached_pool_push(pool, memc);
54 memcached_pool_destroy(pool);
56 In the above code you create a :type:`memcached_pool_st` object with three
57 server by making use of :func:`memcached_pool()`.
59 When :func:`memcached_pool_destroy()` all memory will be released that is associated
62 Adding a value to the server
63 ----------------------------
72 memcached_return_t rc = memcached_set(memc,
77 if (rc != MEMCACHED_SUCCESS)
82 It is best practice to always look at the return value of any operation.
84 Fetching multiple values
85 ------------------------
89 memcached_return_t rc;
90 char *keys[]= {"fudge", "son", "food"};
91 size_t key_length[]= {5, 3, 4};
95 char return_key[MEMCACHED_MAX_KEY];
96 size_t return_key_length;
98 size_t return_value_length;
100 rc= memcached_mget(memc, keys, key_length, 3);
103 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
104 &return_value_length, &flags, &rc)))
110 Notice that you freed values returned from memcached_fetch(). The define
111 `MEMCACHED_MAX_KEY` is provided for usage.
116 :manpage:`memcached(1)`