Merge in all new docs
[m6w6/libmemcached] / docs / libmemcached_examples.rst
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 Creating and Freeing the memcached_st structure
18 -----------------------------------------------
19
20
21 .. code-block:: c
22
23 memcached_st *memc;
24 memcached_return_t rc;
25
26 memc= memcached_create(NULL);
27 ...do stuff...
28 memcached_free(memc);
29
30
31 The above code would create a connection and then free the connection when
32 finished.
33
34
35 ---------------------
36 Connecting to servers
37 ---------------------
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 memcached_st *memc= memcached_create_with_options(config_string, strlen(config_string);
45 {
46 ...
47 }
48 memcached_free(memc);
49
50
51 In the above code you create a \ ``memcached_st``\ object with three server by making use of :manpage:`memcached_create_with_options(3)`.
52
53
54 --------------------------
55 Creating a pool of servers
56 --------------------------
57
58
59
60 .. code-block:: c
61
62 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com";
63
64 memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
65
66 memcached_return_t rc;
67
68 memcached_st *memc= memcached_pool_pop(pool, false, &rc);
69
70 .... do work
71
72 /*
73 Release the memc_ptr that was pulled from the pool
74 */
75 memcached_pool_push(pool, memc);
76
77 /*
78 Destroy the pool.
79 */
80 memcached_pool_destroy(pool);
81
82
83
84 In the above code you create a \ ``memcached_pool_st``\ object with three
85 server by making use of :manpage:`memcached_pool(3)`.
86
87 When memcached_pool_destroy() all memory will be released that is associated
88 with the pool.
89
90
91 ----------------------------
92 Adding a value to the server
93 ----------------------------
94
95
96
97 .. code-block:: c
98
99 char *key= "foo";
100 char *value;
101 size_t value_length= 8191;
102 unsigned int x;
103
104 value = (char*)malloc(value_length);
105 assert(value);
106
107 for (x= 0; x < value_length; x++)
108 value[x] = (char) (x % 127);
109
110 for (x= 0; x < 1; x++)
111 {
112 rc= memcached_set(memc, key, strlen(key),
113 value, value_length,
114 (time_t)0, (uint32_t)0);
115 assert(rc == MEMCACHED_SUCCESS);
116 }
117
118 free(value);
119
120
121 It is best practice to always look at the return value of any operation.
122
123
124 ------------------------
125 Fetching multiple values
126 ------------------------
127
128
129
130 .. code-block:: c
131
132 memcached_return_t rc;
133 char *keys[]= {"fudge", "son", "food"};
134 size_t key_length[]= {5, 3, 4};
135 unsigned int x;
136 uint32_t flags;
137
138 char return_key[MEMCACHED_MAX_KEY];
139 size_t return_key_length;
140 char *return_value;
141 size_t return_value_length;
142
143 rc= memcached_mget(memc, keys, key_length, 3);
144
145 x= 0;
146 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
147 &return_value_length, &flags, &rc)))
148 {
149 free(return_value);
150 x++;
151 }
152
153
154 Notice that you freed values returned from memcached_fetch(). The define
155 \ ``MEMCACHED_MAX_KEY``\ is provided for usage.
156
157
158
159 ----
160 HOME
161 ----
162
163
164 To find out more information please check:
165 `https://launchpad.net/libmemcached <https://launchpad.net/libmemcached>`_
166
167
168 --------
169 SEE ALSO
170 --------
171
172
173 :manpage:`memcached(1)`
174