京东云数据库MemcachedC++客户端

C++客户端:libmemcached客户端介绍:

https://launchpad.net/libmemcached?spm=a2c4g.11186623.2.5.bGUAw2

安装SASL相关环境:yuminstall-ycyrus-sasl-plaincyrus-saslcyrus-sasl-develcyrus-sasl-liblibmemcached安装:

wgethttps://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gztar-xvflibmemcached-1.0.18.tar.gzcdlibmemcached-1.0.18./configure--enable-saslmake#defineIP&amp;quot;ip&amp;quot;#definePORTport//默认为11211intmain(intargc,charargv[]){//connectservermemcached_stmemc;memcached_returnrc;memcached_server_stserver;time_texpiration=0;uint32_tflags;memc=memcached_create(NULL);server=memcached_server_list_append(NULL,IP,PORT,rc=memcached_server_push(memc,server);memcached_server_list_free(server);stringkey=&amp;quot;Key&amp;quot;;stringvalue=&amp;quot;Value&amp;quot;;size_tvalue_length=value.length();size_tkey_length=key.length();rc=memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),expiration,flags);if(rc==MEMCACHED_SUCCESS){cout<<"savekey:"<<key<<",value:"<<value<<",sucessfully!"<<endl;}charresult=memcached_get(memc,key.c_str(),key_length,if(rc==memcached_success){cout<<"getkey:"<<key<<",value:"<<result<<",sucessfully!"<<endl;}rc=memcached_delete(memc,key.c_str(),key_length,expiration);if(rc==memcached_success){cout<<"deletekey:"<<key<<",value:"<<value<<",sucessfully!"<<endl;}else{cout<<rc<<endl;}memcached_free(memc);return0;} C++示例代码2(密码访问): #include<iostream>#include<string>#include<libmemcached/memcached.h>usingnamespacestd;#defineIP&amp;quot;ip&amp;quot;#definePORTport//默认为11211#defineUSERNAME&amp;quot;username&amp;quot;#definePASSWORD&amp;quot;password&amp;quot;intmain(intargc,charargv[]){memcached_stmemc;memcached_returnrc;memcached_server_stserver;time_texpiration=0;uint32_tflags;memc=memcached_create(NULL);server=memcached_server_list_append(NULL,IP,PORT,sasl_client_init(NULL);rc=memcached_set_sasl_auth_data(memc,USERNAME,PASSWORD);if(rc!=MEMCACHED_SUCCESS){cout</p> <p> &nbsp;</p> <p><strong>代码编译链接:</strong>g++-olibmemcachedlibmemcached.cpp-lmemcached-std=c++0x-lpthread-lsasl2</libmemcached/memcached.h></string></iostream>

标签

发表评论