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