当前位置: 首页 > 工具软件 > libmemcached > 使用案例 >

libmemcached用法

齐俊达
2023-12-01

#include <iostream>
#include <string>
#include <libmemcached/memcached.h>
using namespace std;
 
int main(int argc,char *argv[])
{
    //connect server
    memcached_st *memc;
    memcached_return rc;
    memcached_server_st *server;
    time_t expiration;
    uint32_t  flags;
 
    memc = memcached_create(NULL);
    server = memcached_server_list_append(NULL,"127.0.0.1",11211,&rc);
 server = memcached_server_list_append(server,"192.168.116.1",11211,&rc);
    rc=memcached_server_push(memc,server);
 
  if(rc==MEMCACHED_SUCCESS)
    {
       cout<<"Server push sucessful!"<<endl;
    }
    else
    {
       cout << "server push erro " <<rc <<  endl;
    }
    memcached_server_list_free(server);
 
    string key = "key";
    string value = "value";
    size_t value_length = value.length();
    size_t key_length = key.length();
 
 
    //Save data
    rc=memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),(time_t)0, (uint32_t)0);
    if(rc==MEMCACHED_SUCCESS)
    {
        cout<<"Save data:"<<value<<" sucessful!"<<endl;
    }
 else
 {
  cout << "memcached_set erro " <<rc <<  endl;
 }
    //Get data
    char* result = memcached_get(memc,key.c_str(),key_length,&value_length,&flags,&rc);
    if(rc == MEMCACHED_SUCCESS)
    {
         cout<<"Get value:"<<result<<" sucessful!"<<endl;
    }
 else
 {
  cout << "memcached_get erro " <<rc <<  endl;
 }
   
     //Delete data
    rc=memcached_delete(memc,key.c_str(),key_length,expiration);
    if(rc==MEMCACHED_SUCCESS)
    {
        cout<<"Delete key:"<<key<<" sucessful!"<<endl;
    }
 else
 {
  cout << "memcached_delete erro " <<rc <<  endl;
 }
 
     //free
    memcached_free(memc);
    return 0;
}

 类似资料: