Hard to believe I documented the entire thing... this is a first.
[awesomized/libmemcached] / docs / libmemcached_examples.pod
1 =head1 NAME
2
3 libmemcached_examples - Examples for libmemcached
4
5 =head1 DESCRIPTION
6
7 For full example, example test cases 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 =item Creating and Freeing structure
12
13 memcached_st *memc;
14 memcached_return rc;
15 struct timeval start_time, end_time;
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 when
22 finished.
23
24 =item Connecting to servers
25
26 memcached_server_st *servers;
27 memcached_st *memc= memcached_create(NULL);
28 char servername[]= "0.example.com";
29
30 servers= memcached_server_list_append(NULL, servername, 400, &rc);
31
32 for (x= 0; x < 20; x++)
33 {
34 char buffer[SMALL_STRING_LEN];
35
36 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
37 servers= memcached_server_list_append(servers, buffer, 401,
38 &rc);
39 }
40 rc= memcached_server_push(memc, servers);
41 memcached_server_free(servers);
42 memcached_free(memc);
43
44 In the above code we create a memc object that we then feed in a single
45 hosts into. In the for loop we build a memcached_server_st* that we then
46 later feed via memcached_server_push(3) into the memcached_st structure.
47
48 We can reuse the memcached_server_st() object with multile memcached_st
49 structures.
50
51 =item Adding a value to the server
52
53 char *key= "foo";
54 char *value;
55 size_t value_length= 8191;
56 unsigned int x;
57
58 value = (char*)malloc(value_length);
59 assert(value);
60
61 for (x= 0; x < value_length; x++)
62 value[x] = (char) (x % 127);
63
64 for (x= 0; x < 1; x++)
65 {
66 rc= memcached_set(memc, key, strlen(key),
67 value, value_length,
68 (time_t)0, (uint16_t)0);
69 assert(rc == MEMCACHED_SUCCESS);
70 }
71
72 free(value);
73
74 It is best practice to always look at the return value of any operation.
75
76 =item Fetching multiple values
77
78 memcached_return rc;
79 char *keys[]= {"fudge", "son", "food"};
80 size_t key_length[]= {5, 3, 4};
81 unsigned int x;
82 uint16_t flags;
83
84 char return_key[MEMCACHED_MAX_KEY];
85 size_t return_key_length;
86 char *return_value;
87 size_t return_value_length;
88
89 rc= memcached_mget(memc, keys, key_length, 3);
90
91 x= 0;
92 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
93 &return_value_length, &flags, &rc)))
94 {
95 free(return_value);
96 x++;
97 }
98
99 Notice that we freed values returned fromm memcached_fetch(3). The define
100 MEMCACHED_MAX_KEY is provided for usage.
101
102 =head1 HOME
103
104 To find out more information please check: http://tangent.org/552/libmemcached.html
105
106 =head1 AUTHOR
107
108 Brian Aker, brian@tangent.org
109
110 =head1 SEE ALSO
111
112 memcached(1).
113
114 =cut
115