memcached——与c++对接,建立连接

参考官方文档:http://docs.libmemcached.org/index.html

根据菜鸟教程上介绍,c++实现了连接memcached的客户端。

通过apt-cache search,查到到了需要安装的库

root@iZwz9aujzgp9b5o38x29akZ:~# apt-cache search memcached | grep C++
libmemcached-dev - C and C++ client library to the memcached server (development files)
libmemcached11 - C and C++ client library to the memcached server

于是分别安装了libmemcached-dev与libmemcached11

root@iZwz9aujzgp9b5o38x29akZ:~# apt-get install libmemcached11
.....//
root@iZwz9aujzgp9b5o38x29akZ:~# apt-get install libmemcached-dev
.....//

然后找到头文件

root@iZwz9aujzgp9b5o38x29akZ:~# cd /usr/include/libmemcached/
root@iZwz9aujzgp9b5o38x29akZ:/usr/include/libmemcached# ll
total 20
drwxr-xr-x  2 root root 4096 Jan 19 19:16 ./
drwxr-xr-x 40 root root 4096 Jan 19 19:15 ../
-rw-r--r--  1 root root 1713 Jan 18  2019 memcached.h
-rw-r--r--  1 root root 1717 Jan 18  2019 memcached.hpp
-rw-r--r--  1 root root 1714 Jan 18  2019 util.h

与libmemcached官方文档相符

 

接下来就是要写个测试程序,连上memcached

根据官方文档的资源,写了如下测试程序

vim connect_to_memcached.cpp 
 1 #include 
 2   
 3 #include <string.h>
 4 #include 
 5 
 6 using namespace std;
 7 int main()
 8 {
 9         const char * config_string = "--SERVER=127.0.0.1";
10         memcached_st * memc = NULL;
11         cout << static_cast<const void *>(memc) << endl;
12         memc = memcached(config_string, strlen(config_string));
13         cout << static_cast<const void *>(memc) << endl;
14         memcached_free(memc);
15         cout << static_cast<const void *>(memc) << endl;
16         return 0;
17 }

编译

g++ connect_to_memcached.cpp -o connect_to_memcached.exe            
/tmp/ccf2s5UM.o: In function `main':
connect_to_memcached.cpp:(.text+0x2d): undefined reference to `memcached'
connect_to_memcached.cpp:(.text+0x3d): undefined reference to `memcached_free'
collect2: error: ld returned 1 exit status

这里报错了,是因为没有链接其动态库,最终链接失败

g++ connect_to_memcached.cpp -lmemcached -o connect_to_memcached.exe

编译成功。执行./connect_to_memcached.exe 

root@iZwz9aujzgp9b5o38x29akZ:~/memcached_cpp# ./connect_to_memcached.exe 
0
0x557ca3ff3280
0x557ca3ff3280

说明memcached()给其分配了空间。为了证明连接上了,需要再从memcache里取点数据看看

 1 #include 
 2 
 3 #include <string.h>
 4 #include 
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10         const char * config_string = "--SERVER=127.0.0.1";
11         memcached_st * memc = memcached(config_string, strlen(config_string)); 
12         memcached_return_t ret;
13         const char * key = "name";
14         size_t value_len;
15         uint32_t flags;
16         char * value = memcached_get(memc, key, strlen(key), &value_len, &flags, &ret);
17         cout << "key:" << key << endl;
18         cout << "ret:" << ret << endl;
19 
20         if(memcached_success(ret))
21         {
22                 cout << "value:" << value << endl;
23                 cout << "value_len:" << value_len << endl;
24                 cout << "flags:" << flags << endl;
25         }
26         else
27         {
28                 cout << "memcached_get fail" << endl;
29         }
30 
31         memcached_free(memc);
32         return 0;
33 }
root@iZwz9aujzgp9b5o38x29akZ:~/memcached_cpp# g++ connect_to_memcached.cpp -lmemcached -o connect_to_memcached.exe
root@iZwz9aujzgp9b5o38x29akZ:~/memcached_cpp# ./connect_to_memcached.exe 
key:name
ret:0
value:ccx
value_len:3
flags:0

你可能感兴趣的:(memcached——与c++对接,建立连接)