memcached有很多版本的Client,具体可参照:http://code.google.com/p/memcached/wiki/Clients
本次只讨论libmemcached这个client的安装及使用
官网:http://libmemcached.org/libMemcached.html
libMemcached was designed to provide the greatest number of options to use Memcached. Some of the features provided:
-
Asynchronous and Synchronous Transport Support.
-
Consistent Hashing and Distribution.
-
Tunable Hashing algorithm to match keys.
-
Access to large object support.
-
Local replication.
-
A complete reference guide and documentation to the API.
-
Tools to Manage your Memcached networks.
0.52版本:
http://launchpad.net/libmemcached/1.0/0.52/+download/libmemcached-0.52.tar.gz
1、安装
tar zxvf libmemcached-0.52.tar.gz
cd libmemcached-0.52
./configure
make
make install
2、测试
1 #include<iostream>
2 #include<string>
3 #include<libmemcached/memcached.h>
4
5 using namespace std;
6
7 int main(int argc, char* argv[])
8 {
9 // connect server
10 memcached_st *memc;
11 memcached_return rc;
12 memcached_server_st* server;
13 time_t expiration = 0;
14 uint32_t flags;
15
16 memc = memcached_create(NULL);
17 server = memcached_server_list_append(NULL, "localhost", 11211, &rc);
18 rc = memcached_server_push(memc, server);
19 memcached_server_list_free(server);
20
21 server = memcached_server_list_append(NULL, "localhost", 11212, &rc);
22 rc = memcached_server_push(memc, server);
23 memcached_server_list_free(server);
24
25 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
26
27 string key = "keyvalue";
28 string value = "value";
29 size_t value_length = value.length();
30 size_t key_length = key.length();
31
32 // save data
33 rc = memcached_set(memc, key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags);
34 if(rc == MEMCACHED_SUCCESS)
35 {
36 cout<<"Save data "<<value<<" successful!!"<<endl;
37 }
38
39 char* result = memcached_get(memc, key.c_str(), key_length, &value_length, &flags, &rc);
40 if(rc == MEMCACHED_SUCCESS)
41 {
42 cout<<"Get Value "<<result<<" successful!!"<<endl;
43 free(result);
44 }
45
46 // delete data
47 rc = memcached_delete(memc, key.c_str(), key_length, expiration);
48 if ( rc == MEMCACHED_SUCCESS)
49 {
50 cout<<" Delete key:"<<key<<" successful!!"<<endl;
51 }
52
53 // free
54 memcached_free(memc);
55 return 0;
56 }
g++ -o testmemcached testmemcached.cpp -lmemcached
[ybt@mj247 memcached]$ ./testmemcached
Save data value successful!!
Get Value value successful!!
Delete key:keyvalue successful!!
此时在server端上有输出:
[root@mj247 memcached]# <28 new auto-negotiating client connection
28: Client using the ascii protocol
<28 set keyvalue 0 0 5
>28 STORED
<28 get keyvalue
>28 sending key keyvalue
>28 END
<28 delete keyvalue
>28 DELETED
<28 quit
<28 connection closed.
由于memcached支持分布式,所以在libmemcached上会根据key值做hash来得到需要存储在哪个server上。代码上memcached server分别11211及11212这两个端口来测试。
2013-07-01更新
1.0.17版本安装
会报错:
error: tr1/cinttypes: No such file or directory
要求gcc版本比较高
# yum install gcc44 gcc44-c++ libstdc++44-devel # export CC=/usr/bin/gcc44 # export CXX=/usr/bin/g++44
# ./configure --enable-tcmalloc