1LIBMEMCACHED_EXAMPLES(3) libmemcached 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= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com"
17 memcached_st *memc= memcached(config_string, strlen(config_string);
18 {
19 ...
20 }
21 memcached_free(memc);
22
23 In the above code you create a memcached_st object with three server by
24 making use of memcached_create().
25
27 Creating a pool of Servers:
28
29 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com";
30
31 memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
32
33 memcached_return_t rc;
34
35 memcached_st *memc= memcached_pool_pop(pool, false, &rc);
36
37 .... do work
38
39 /*
40 Release the memc_ptr that was pulled from the pool
41 */
42 memcached_pool_push(pool, memc);
43
44 /*
45 Destroy the pool.
46 */
47 memcached_pool_destroy(pool);
48
49 In the above code you create a memcached_pool_st object with three
50 server by making use of memcached_pool().
51
52 When memcached_pool_destroy() all memory will be released that is asso‐
53 ciated with the pool.
54
56 Adding a value to the Server:
57
58 char *key= "foo";
59 char *value= "value";
60
61 memcached_return_t rc= memcached_set(memc, key, strlen(key), value, value_length, (time_t)0, (uint32_t)0);
62
63 if (rc != MEMCACHED_SUCCESS)
64 {
65 ... // handle failure
66 }
67
68 It is best practice to always look at the return value of any opera‐
69 tion.
70
72 memcached_return_t rc;
73 char *keys[]= {"fudge", "son", "food"};
74 size_t key_length[]= {5, 3, 4};
75 unsigned int x;
76 uint32_t flags;
77
78 char return_key[MEMCACHED_MAX_KEY];
79 size_t return_key_length;
80 char *return_value;
81 size_t return_value_length;
82
83 rc= memcached_mget(memc, keys, key_length, 3);
84
85 x= 0;
86 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
87 &return_value_length, &flags, &rc)))
88 {
89 free(return_value);
90 x++;
91 }
92
93 Notice that you freed values returned from memcached_fetch(). The
94 define MEMCACHED_MAX_KEY is provided for usage.
95
97 To find out more information please check: http://libmemcached.org/
98
100 memcached(1)
101
103 Brian Aker
104
106 2011-2013, Brian Aker DataDifferential, http://datadifferential.com/
107
108
109
110
1111.0.18 February 09, 2014 LIBMEMCACHED_EXAMPLES(3)