Memcached libmemcached install for centos

 在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 ios

 4 安裝libmemcachedapp

   $ 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 installmemcached

默認的安裝路徑頭文件或者C++ 的源文件/usr/local/include/libmemcachde/xxxxx函數

動態鏈接庫的目錄:/usr/local/lib/測試

編譯方法 g++ -o  demo.cpp -lmemcachedui

運行前先導出動態連接庫的路徑到$LD_LIBRARY_PATH,spa

例如 export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH.net

demo.cppserver

 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文件中。

相關文章
相關標籤/搜索