3
libmemcached_examples - Examples for libmemcached
7
For full examples, test cases are found in tests/*.c in the main
8
distribution. These are always up to date, and are used for each test run of
11
=head2 Creating and Freeing structure
14
memcached_return_t rc;
16
memc= memcached_create(NULL);
20
The above code would create a connection and then free the connection when
23
=head2 Connecting to servers
25
memcached_server_st *servers;
26
memcached_st *memc= memcached_create(NULL);
27
char servername[]= "0.example.com";
29
servers= memcached_server_list_append(NULL, servername, 400, &rc);
31
for (x= 0; x < 20; x++)
33
char buffer[SMALL_STRING_LEN];
35
snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
36
servers= memcached_server_list_append(servers, buffer, 401, &rc);
38
rc= memcached_server_push(memc, servers);
39
memcached_server_free(servers);
42
In the above code you create a C<memcached_st> object that you then feed in a
43
single host into. In the for loop you build a C<memcached_server_st>
44
pointer that you then later feed via memcached_server_push() into the
45
C<memcached_st> structure.
47
You can reuse the C<memcached_server_st> object with multile C<memcached_st>
50
=head2 Adding a value to the server
54
size_t value_length= 8191;
57
value = (char*)malloc(value_length);
60
for (x= 0; x < value_length; x++)
61
value[x] = (char) (x % 127);
63
for (x= 0; x < 1; x++)
65
rc= memcached_set(memc, key, strlen(key),
67
(time_t)0, (uint32_t)0);
68
assert(rc == MEMCACHED_SUCCESS);
73
It is best practice to always look at the return value of any operation.
75
=head2 Fetching multiple values
77
memcached_return_t rc;
78
char *keys[]= {"fudge", "son", "food"};
79
size_t key_length[]= {5, 3, 4};
83
char return_key[MEMCACHED_MAX_KEY];
84
size_t return_key_length;
86
size_t return_value_length;
88
rc= memcached_mget(memc, keys, key_length, 3);
91
while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
92
&return_value_length, &flags, &rc)))
98
Notice that you freed values returned from memcached_fetch(). The define
99
C<MEMCACHED_MAX_KEY> is provided for usage.
103
To find out more information please check:
104
L<https://launchpad.net/libmemcached>
108
Brian Aker, E<lt>brian@tangent.orgE<gt>