Download and Installation
Once you have downloaded the libmemcached package, you need to extract and compile the library:
$ tar xjf libmemcached-0.47.tar.gz
$ cd libmemcached-0.47
$ ./configure
$ make
$ make install
Once installed, you can build and compile applications utilizing the library directly.
Sample Client
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libmemcached/memcached.h>
int main(int argc, char *argv[])
{
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char *key = "samplekey";
char *value = "samplestring";
char *retvalue = NULL;
size_t retlength;
uint32_t flags;
/* Create an empty memcached interface */
memc = memcached_create(NULL);
/* Append a server to the list */
servers = memcached_server_list_append(servers, "192.168.0.2", 11211, &rc);
/* Update the memcached structure with the updated server list */
rc = memcached_server_push(memc, servers);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr,"Successfully added server\n");
else
fprintf(stderr,"Couldn't add server: %s\n",memcached_strerror(memc, rc));
/* Store a value, without a timelimit */
rc = memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr,"Value stored successfully\n");
else
fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc));
/* Retrieve the Stored value */
retvalue = memcached_get(memc, key, strlen(key), &retlength, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr,"Value retrieved successfully: %s\n",retvalue);
else
fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc));
return 0;
}
Build
To compile, you must specify the libmemcached library during the linking phase:
$ gcc sample.c -lmemcached -o sample
You can now execute the sample code:
$ ./sample
Successfully added server
Value stored successfully
Value retrieved successfully: samplestring