update docs
[m6w6/libmemcached] / _sources / libmemcached_examples.rst.txt
1 ========
2 Examples
3 ========
4
5 Examples for libmemcached
6
7 -----------
8 DESCRIPTION
9 -----------
10
11
12 For full examples, test cases are found in tests/\*.c in the main
13 distribution. These are always up to date, and are used for each test run of
14 the library.
15
16
17 ---------------------
18 Connecting to servers
19 ---------------------
20
21
22 .. code-block:: c
23
24 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com"
25 memcached_st *memc= memcached(config_string, strlen(config_string);
26 {
27 ...
28 }
29 memcached_free(memc);
30
31
32 In the above code you create a :c:type:`memcached_st` object with three server
33 by making use of :c:func:`memcached_create`.
34
35
36 --------------------------
37 Creating a pool of servers
38 --------------------------
39
40
41 .. code-block:: c
42
43 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com";
44
45 memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
46
47 memcached_return_t rc;
48
49 memcached_st *memc= memcached_pool_pop(pool, false, &rc);
50
51 .... do work
52
53 /*
54 Release the memc_ptr that was pulled from the pool
55 */
56 memcached_pool_push(pool, memc);
57
58 /*
59 Destroy the pool.
60 */
61 memcached_pool_destroy(pool);
62
63
64
65 In the above code you create a :c:type:`memcached_pool_st` object with three
66 server by making use of :c:func:`memcached_pool()`.
67
68 When :c:func:`memcached_pool_destroy()` all memory will be released that is associated
69 with the pool.
70
71
72 ----------------------------
73 Adding a value to the server
74 ----------------------------
75
76
77
78 .. code-block:: c
79
80 char *key= "foo";
81 char *value= "value";
82
83 memcached_return_t rc= memcached_set(memc, key, strlen(key), value, value_length, (time_t)0, (uint32_t)0);
84
85 if (rc != MEMCACHED_SUCCESS)
86 {
87 ... // handle failure
88 }
89
90
91 It is best practice to always look at the return value of any operation.
92
93
94 ------------------------
95 Fetching multiple values
96 ------------------------
97
98
99
100 .. code-block:: c
101
102 memcached_return_t rc;
103 char *keys[]= {"fudge", "son", "food"};
104 size_t key_length[]= {5, 3, 4};
105 unsigned int x;
106 uint32_t flags;
107
108 char return_key[MEMCACHED_MAX_KEY];
109 size_t return_key_length;
110 char *return_value;
111 size_t return_value_length;
112
113 rc= memcached_mget(memc, keys, key_length, 3);
114
115 x= 0;
116 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
117 &return_value_length, &flags, &rc)))
118 {
119 free(return_value);
120 x++;
121 }
122
123
124 Notice that you freed values returned from memcached_fetch(). The define
125 :c:type:`MEMCACHED_MAX_KEY` is provided for usage.
126
127
128
129 ----
130 HOME
131 ----
132
133
134 To find out more information please check:
135 `http://libmemcached.org/ <http://libmemcached.org/>`_
136
137
138 --------
139 SEE ALSO
140 --------
141
142
143 :manpage:`memcached(1)`
144