1 |
#include <stdio.h> |
2 |
#include <string.h> |
3 |
#include <time.h> |
4 |
#include <libmemcached/memcached.h> |
5 |
|
6 |
const int PORT = 11211; |
7 |
const int TIMEOUT = 60; |
8 |
|
9 |
int main() { |
10 |
memcached_st memc; |
11 |
memcached_create( &memc );//init structure |
12 |
|
13 |
memcached_server_add(&memc, "localhost", PORT); |
14 |
|
15 |
const char* key = "mykey"; |
16 |
|
17 |
srand(time(0)); |
18 |
|
19 |
char value[256]; |
20 |
sprintf(value,"This is the cached value %i", rand() ); |
21 |
|
22 |
memcached_set(&memc, key, strlen(key), value, strlen(value)+1, TIMEOUT, 0); //the +1 is just to include trailing \0 |
23 |
printf("wrote: %s\n", value); |
24 |
|
25 |
unsigned int size; |
26 |
unsigned int flags; |
27 |
memcached_return errors; |
28 |
char* returned = memcached_get(&memc, key, strlen(key), &size, &flags, &errors); |
29 |
|
30 |
printf("Returned: %s\n", returned); |
31 |
|
32 |
return 0; |
33 |
} |