Merge in all of build.
[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 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 Adding a value to the server
56 ----------------------------
57
58
59
60 .. code-block:: c
61
62 char *key= "foo";
63 char *value;
64 size_t value_length= 8191;
65 unsigned int x;
66
67 value = (char*)malloc(value_length);
68 assert(value);
69
70 for (x= 0; x < value_length; x++)
71 value[x] = (char) (x % 127);
72
73 for (x= 0; x < 1; x++)
74 {
75 rc= memcached_set(memc, key, strlen(key),
76 value, value_length,
77 (time_t)0, (uint32_t)0);
78 assert(rc == MEMCACHED_SUCCESS);
79 }
80
81 free(value);
82
83
84 It is best practice to always look at the return value of any operation.
85
86
87 ------------------------
88 Fetching multiple values
89 ------------------------
90
91
92
93 .. code-block:: c
94
95 memcached_return_t rc;
96 char *keys[]= {"fudge", "son", "food"};
97 size_t key_length[]= {5, 3, 4};
98 unsigned int x;
99 uint32_t flags;
100
101 char return_key[MEMCACHED_MAX_KEY];
102 size_t return_key_length;
103 char *return_value;
104 size_t return_value_length;
105
106 rc= memcached_mget(memc, keys, key_length, 3);
107
108 x= 0;
109 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
110 &return_value_length, &flags, &rc)))
111 {
112 free(return_value);
113 x++;
114 }
115
116
117 Notice that you freed values returned from memcached_fetch(). The define
118 \ ``MEMCACHED_MAX_KEY``\ is provided for usage.
119
120
121
122 ----
123 HOME
124 ----
125
126
127 To find out more information please check:
128 `https://launchpad.net/libmemcached <https://launchpad.net/libmemcached>`_
129
130
131 --------
132 SEE ALSO
133 --------
134
135
136 :manpage:`memcached(1)`
137