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