1LIBMEMCACHED_EXAMPLES(3) libmemcached-awesome LIBMEMCACHED_EXAMPLES(3)
2
3
4
6 libmemcached_examples - libmemcached Documentation
7
8 Examples for libmemcached
9
11 For full examples, test cases are found in tests/*.c in the main dis‐
12 tribution. These are always up to date, and are used for each test run
13 of the library.
14
16 const char *config_string =
17 "--SERVER=host10.example.com "
18 "--SERVER=host11.example.com "
19 "--SERVER=host10.example.com";
20 memcached_st *memc= memcached(config_string, strlen(config_string);
21 {
22 // ...
23 }
24 memcached_free(memc);
25
26 In the above code you create a memcached_st object with three server by
27 making use of memcached_create().
28
30 const char *config_string =
31 "--SERVER=host10.example.com "
32 "--SERVER=host11.example.com "
33 "--SERVER=host10.example.com";
34
35 memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
36
37 memcached_return_t rc;
38
39 memcached_st *memc= memcached_pool_pop(pool, false, &rc);
40
41 // .... do work
42
43 /*
44 Release the memc_ptr that was pulled from the pool
45 */
46 memcached_pool_push(pool, memc);
47
48 /*
49 Destroy the pool.
50 */
51 memcached_pool_destroy(pool);
52
53 In the above code you create a memcached_pool_st object with three
54 server by making use of memcached_pool().
55
56 When memcached_pool_destroy() all memory will be released that is asso‐
57 ciated with the pool.
58
60 char *key= "foo";
61 char *value= "value";
62 time_t expires = 0;
63 uint32_t flags = 0;
64
65 memcached_return_t rc = memcached_set(memc,
66 key, strlen(key),
67 value, value_length,
68 expires, flags);
69
70 if (rc != MEMCACHED_SUCCESS)
71 {
72 // handle failure
73 }
74
75 It is best practice to always look at the return value of any opera‐
76 tion.
77
79 memcached_return_t rc;
80 char *keys[]= {"fudge", "son", "food"};
81 size_t key_length[]= {5, 3, 4};
82 unsigned int x;
83 uint32_t flags;
84
85 char return_key[MEMCACHED_MAX_KEY];
86 size_t return_key_length;
87 char *return_value;
88 size_t return_value_length;
89
90 rc= memcached_mget(memc, keys, key_length, 3);
91
92 x= 0;
93 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
94 &return_value_length, &flags, &rc)))
95 {
96 free(return_value);
97 x++;
98 }
99
100 Notice that you freed values returned from memcached_fetch(). The de‐
101 fine MEMCACHED_MAX_KEY is provided for usage.
102
104 memcached(1)
105
106
107
108
1091.1 Feb 02, 2022 LIBMEMCACHED_EXAMPLES(3)