在CentOS下安装Memcached和需要的libevent。
1. CentOS 开发环境一键安装 :
yum groupinstall 'Development Tools'
2. 安装libevent
$ wget http://www.monkey.org/~provos/libevent-1.4.8-stable.tar.gz
$ tar xzf libevent-1.4.8-stable.tar.gz
$ cd libevent-1.4.8-stable
$ ./configure
$ make
$ make install
3. 安装memcached
$ wget http://memcached.org/latest
$ tar xzf memcached-1.x.x.tar.gz
$ cd memcached-1.x.x
$ ./configure
$ make
$ make install
测试是否安装成功:
$ memcached -u nobody -d -m 64 -p 11211
4 安装libmemcached
$ wget http://launchpad.net/libmemcached/1.0/0.44/+download/libmemcached-0.44.tar.gz //可以搜索最新版本的下载
$ tar xvzf libmemcached-0.44tar.gz
$ cd libmemcached-0.44
$ ./configure
$ make
$ sudo make install
默认的安装路径头文件或者C++ 的源文件/usr/local/include/libmemcachde/xxxxx
动态连接库的目录:/usr/local/lib/
编译方法 g++ -o demo.cpp -lmemcached
运行前先导出动态链接库的路径到$LD_LIBRARY_PATH,
例如 export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
demo.cpp
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 = 0;
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 string key = "key";
22 string value = "value";
23 size_t value_length = value.length();
24 size_t key_length = key.length();
25
26
27 //Save data
28 rc=memcached_set(memc,key.c_str(), key_length, value.c_str(),value.length,expiration,flags);
29 if(rc==MEMCACHED_SUCCESS)
30 {
31 cout<<"Save data:"<<value<<" sucessful!"<<endl;
32 }
33
34 //Get data
35 char* result = memcached_get(memc,key.c_str(),key_length,&value_length,&flags,&rc);
36 if(rc == MEMCACHED_SUCCESS)
37 {
38 cout<<"Get value:"<<result<<" sucessful!"<<endl;
39 }
40
41 //Delete data
42 rc=memcached_delete(memc,key.c_str(),key_length,expiration);
43 if(rc==MEMCACHED_SUCCESS)
44 {
45 cout<<"Delete key:"<<key<<" sucessful!"<<endl;
46 }
47
48 //free
49 memcached_free(memc);
50 return 0;
51 }
//运行成功,表示安装完成。
//可以使用里面封装好的类和函数,在/usr/local/include/libmemcached-1.0/memchachXXXXX文件中。