New docs system.
[m6w6/libmemcached] / docs / libmemcached_examples.rst
1 .. highlight:: perl
2
3
4 libmemcached_examples
5 *********************
6
7
8 Examples for libmemcached
9
10
11 ***********
12 DESCRIPTION
13 ***********
14
15
16 For full examples, test cases are found in tests/\*.c in the main
17 distribution. These are always up to date, and are used for each test run of
18 the library.
19
20 Creating and Freeing structure
21 ==============================
22
23
24
25 .. code-block:: perl
26
27 memcached_st *memc;
28 memcached_return_t rc;
29
30 memc= memcached_create(NULL);
31 ...do stuff...
32 memcached_free(memc);
33
34
35 The above code would create a connection and then free the connection when
36 finished.
37
38
39 Connecting to servers
40 =====================
41
42
43
44 .. code-block:: perl
45
46 memcached_server_st *servers;
47 memcached_st *memc= memcached_create(NULL);
48 char servername[]= "0.example.com";
49
50 servers= memcached_server_list_append(NULL, servername, 400, &rc);
51
52 for (x= 0; x < 20; x++)
53 {
54 char buffer[SMALL_STRING_LEN];
55
56 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
57 servers= memcached_server_list_append(servers, buffer, 401, &rc);
58 }
59 rc= memcached_server_push(memc, servers);
60 memcached_server_free(servers);
61 memcached_free(memc);
62
63
64 In the above code you create a \ ``memcached_st``\ object that you then feed in a
65 single host into. In the for loop you build a \ ``memcached_server_st``\
66 pointer that you then later feed via memcached_server_push() into the
67 \ ``memcached_st``\ structure.
68
69 You can reuse the \ ``memcached_server_st``\ object with multile \ ``memcached_st``\
70 structures.
71
72
73 Adding a value to the server
74 ============================
75
76
77
78 .. code-block:: perl
79
80 char *key= "foo";
81 char *value;
82 size_t value_length= 8191;
83 unsigned int x;
84
85 value = (char*)malloc(value_length);
86 assert(value);
87
88 for (x= 0; x < value_length; x++)
89 value[x] = (char) (x % 127);
90
91 for (x= 0; x < 1; x++)
92 {
93 rc= memcached_set(memc, key, strlen(key),
94 value, value_length,
95 (time_t)0, (uint32_t)0);
96 assert(rc == MEMCACHED_SUCCESS);
97 }
98
99 free(value);
100
101
102 It is best practice to always look at the return value of any operation.
103
104
105 Fetching multiple values
106 ========================
107
108
109
110 .. code-block:: perl
111
112 memcached_return_t rc;
113 char *keys[]= {"fudge", "son", "food"};
114 size_t key_length[]= {5, 3, 4};
115 unsigned int x;
116 uint32_t flags;
117
118 char return_key[MEMCACHED_MAX_KEY];
119 size_t return_key_length;
120 char *return_value;
121 size_t return_value_length;
122
123 rc= memcached_mget(memc, keys, key_length, 3);
124
125 x= 0;
126 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
127 &return_value_length, &flags, &rc)))
128 {
129 free(return_value);
130 x++;
131 }
132
133
134 Notice that you freed values returned from memcached_fetch(). The define
135 \ ``MEMCACHED_MAX_KEY``\ is provided for usage.
136
137
138
139 ****
140 HOME
141 ****
142
143
144 To find out more information please check:
145 `https://launchpad.net/libmemcached <https://launchpad.net/libmemcached>`_
146
147
148 ******
149 AUTHOR
150 ******
151
152
153 Brian Aker, <brian@tangent.org>
154
155
156 ********
157 SEE ALSO
158 ********
159
160
161 memcached(1)
162