Merge Trond
[m6w6/libmemcached] / docs / libmemcached_examples.pod
1 =head1 NAME
2
3 libmemcached_examples - Examples for libmemcached
4
5 =head1 DESCRIPTION
6
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
9 the library.
10
11 =head2 Creating and Freeing structure
12
13 memcached_st *memc;
14 memcached_return rc;
15
16 memc= memcached_create(NULL);
17 ...do stuff...
18 memcached_free(memc);
19
20 The above code would create a connection and then free the connection when
21 finished.
22
23 =head2 Connecting to servers
24
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 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.
46
47 You can reuse the C<memcached_server_st> object with multile C<memcached_st>
48 structures.
49
50 =head2 Adding a value to the server
51
52 char *key= "foo";
53 char *value;
54 size_t value_length= 8191;
55 unsigned int x;
56
57 value = (char*)malloc(value_length);
58 assert(value);
59
60 for (x= 0; x < value_length; x++)
61 value[x] = (char) (x % 127);
62
63 for (x= 0; x < 1; x++)
64 {
65 rc= memcached_set(memc, key, strlen(key),
66 value, value_length,
67 (time_t)0, (uint32_t)0);
68 assert(rc == MEMCACHED_SUCCESS);
69 }
70
71 free(value);
72
73 It is best practice to always look at the return value of any operation.
74
75 =head2 Fetching multiple values
76
77 memcached_return rc;
78 char *keys[]= {"fudge", "son", "food"};
79 size_t key_length[]= {5, 3, 4};
80 unsigned int x;
81 uint32_t flags;
82
83 char return_key[MEMCACHED_MAX_KEY];
84 size_t return_key_length;
85 char *return_value;
86 size_t return_value_length;
87
88 rc= memcached_mget(memc, keys, key_length, 3);
89
90 x= 0;
91 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
92 &return_value_length, &flags, &rc)))
93 {
94 free(return_value);
95 x++;
96 }
97
98 Notice that you freed values returned from memcached_fetch(). The define
99 C<MEMCACHED_MAX_KEY> is provided for usage.
100
101 =head1 HOME
102
103 To find out more information please check:
104 L<http://tangent.org/552/libmemcached.html>
105
106 =head1 AUTHOR
107
108 Brian Aker, E<lt>brian@tangent.orgE<gt>
109
110 =head1 SEE ALSO
111
112 memcached(1)
113
114 =cut
115