116 lines
2.7 KiB
C
116 lines
2.7 KiB
C
|
|
/*********************************************************************
|
||
|
|
*
|
||
|
|
* 文 件: MemcachedClient memcached client
|
||
|
|
*
|
||
|
|
* 版权所有: Shanghai Baosight Software Co., Ltd.
|
||
|
|
*
|
||
|
|
* 概述: memcached client using libmemcached
|
||
|
|
* :
|
||
|
|
* :
|
||
|
|
*
|
||
|
|
* 版本历史
|
||
|
|
* 1.0 2019-01-29 wu.x.h created
|
||
|
|
*
|
||
|
|
*********************************************************************/
|
||
|
|
|
||
|
|
#ifndef MEMCACHEDCLIENT
|
||
|
|
|
||
|
|
#define MEMCACHEDCLIENT
|
||
|
|
|
||
|
|
#include <glob/ConfigMag.h>
|
||
|
|
#include <libmemcached/memcached.h>
|
||
|
|
#include <log4cplus/LOG.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <time.h>
|
||
|
|
#include <iostream>
|
||
|
|
|
||
|
|
using std::cout;
|
||
|
|
using std::endl;
|
||
|
|
using std::string;
|
||
|
|
using namespace baosight;
|
||
|
|
using namespace log4cplus;
|
||
|
|
|
||
|
|
class MemCachedClient {
|
||
|
|
public:
|
||
|
|
~MemCachedClient() { memcached_free(memc); };
|
||
|
|
|
||
|
|
MemCachedClient() {
|
||
|
|
LOG d("MemCachedClient()");
|
||
|
|
|
||
|
|
memcached_return rc;
|
||
|
|
memcached_server_st* server = NULL;
|
||
|
|
memc = memcached_create(NULL);
|
||
|
|
// cout <<"connect to memcached server "<< endl;
|
||
|
|
// server =memcached_server_list_append(server, "127.0.0.1",
|
||
|
|
// 11211, &rc);
|
||
|
|
ConfigMag configmag("CRGS");
|
||
|
|
string ip = configmag.ReadProperty("MemCache", "server");
|
||
|
|
server = memcached_server_list_append(server, ip.c_str(), 11311, &rc);
|
||
|
|
rc = memcached_server_push(memc, server);
|
||
|
|
|
||
|
|
d.Info() << "ip:" << ip << endl;
|
||
|
|
if (MEMCACHED_SUCCESS != rc) {
|
||
|
|
d.Info() << "memcached_server_push failed! rc: " << rc << endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
memcached_server_list_free(server);
|
||
|
|
};
|
||
|
|
|
||
|
|
int Insert(const char* key, const char* value, time_t expiration = 0) {
|
||
|
|
if (NULL == key || NULL == value) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint32_t flags = 32; // 32 for java
|
||
|
|
|
||
|
|
memcached_return rc;
|
||
|
|
// cout <<"set data begin "<< endl;
|
||
|
|
|
||
|
|
rc = memcached_set(memc, key, strlen(key), value, strlen(value), expiration,
|
||
|
|
flags);
|
||
|
|
|
||
|
|
// insert ok
|
||
|
|
if (MEMCACHED_SUCCESS == rc) {
|
||
|
|
// cout <<"set data ok "<< endl;
|
||
|
|
return 1;
|
||
|
|
} else {
|
||
|
|
// cout <<"set data fail "<< endl;
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
string Get(const char* key) {
|
||
|
|
if (NULL == key) {
|
||
|
|
return "no key";
|
||
|
|
}
|
||
|
|
|
||
|
|
uint32_t flags = 0;
|
||
|
|
|
||
|
|
memcached_return rc;
|
||
|
|
|
||
|
|
size_t value_length;
|
||
|
|
// cout <<"get data begin "<< endl;
|
||
|
|
char* value =
|
||
|
|
memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);
|
||
|
|
if (value == NULL) {
|
||
|
|
return "no value";
|
||
|
|
}
|
||
|
|
string ret = string(value);
|
||
|
|
delete value;
|
||
|
|
// get ok
|
||
|
|
if (rc == MEMCACHED_SUCCESS) {
|
||
|
|
// cout <<"get data ok "<< endl;
|
||
|
|
return ret;
|
||
|
|
}
|
||
|
|
|
||
|
|
return "no value";
|
||
|
|
};
|
||
|
|
|
||
|
|
private:
|
||
|
|
memcached_st* memc;
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif
|