1libmemcached_examples.pop(3) libmemcached_exampleslibmemcached_examples.pop(3)
2
3
4
6 libmemcached_examples - Examples for libmemcached
7
9 For full examples, test cases are found in tests/*.c in the main
10 distribution. These are always up to date, and are used for each test
11 run of the library.
12
13 Creating and Freeing structure
14 memcached_st *memc;
15 memcached_return_t rc;
16
17 memc= memcached_create(NULL);
18 ...do stuff...
19 memcached_free(memc);
20
21 The above code would create a connection and then free the connection
22 when finished.
23
24 Connecting to servers
25 memcached_server_st *servers;
26 memcached_st *memc= memcached_create(NULL);
27 char servername[]= "0.example.com";
28
29 servers= memcached_server_list_append(NULL, servername, 400, &rc);
30
31 for (x= 0; x < 20; x++)
32 {
33 char buffer[SMALL_STRING_LEN];
34
35 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
36 servers= memcached_server_list_append(servers, buffer, 401, &rc);
37 }
38 rc= memcached_server_push(memc, servers);
39 memcached_server_free(servers);
40 memcached_free(memc);
41
42 In the above code you create a "memcached_st" object that you then feed
43 in a single host into. In the for loop you build a
44 "memcached_server_st" pointer that you then later feed via
45 memcached_server_push() into the "memcached_st" structure.
46
47 You can reuse the "memcached_server_st" object with multile
48 "memcached_st" structures.
49
50 Adding a value to the server
51 char *key= "foo";
52 char *value;
53 size_t value_length= 8191;
54 unsigned int x;
55
56 value = (char*)malloc(value_length);
57 assert(value);
58
59 for (x= 0; x < value_length; x++)
60 value[x] = (char) (x % 127);
61
62 for (x= 0; x < 1; x++)
63 {
64 rc= memcached_set(memc, key, strlen(key),
65 value, value_length,
66 (time_t)0, (uint32_t)0);
67 assert(rc == MEMCACHED_SUCCESS);
68 }
69
70 free(value);
71
72 It is best practice to always look at the return value of any
73 operation.
74
75 Fetching multiple values
76 memcached_return_t rc;
77 char *keys[]= {"fudge", "son", "food"};
78 size_t key_length[]= {5, 3, 4};
79 unsigned int x;
80 uint32_t flags;
81
82 char return_key[MEMCACHED_MAX_KEY];
83 size_t return_key_length;
84 char *return_value;
85 size_t return_value_length;
86
87 rc= memcached_mget(memc, keys, key_length, 3);
88
89 x= 0;
90 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
91 &return_value_length, &flags, &rc)))
92 {
93 free(return_value);
94 x++;
95 }
96
97 Notice that you freed values returned from memcached_fetch(). The
98 define "MEMCACHED_MAX_KEY" is provided for usage.
99
101 To find out more information please check:
102 <https://launchpad.net/libmemcached>
103
105 Brian Aker, <brian@tangent.org>
106
108 memcached(1)
109
110
111
112 2010-06-28 libmemcached_examples.pop(3)