对 libmemcached进行二次封装,主要是利用其存储 vector<T>。
下面有待改善和完善,先记着,下次更新。
1 #include <iostream> 2 #include <string> 3 #include <libmemcached/memcached.h> 4 #include <vector> 5 6 using namespace std; 7 8 class MemcachedHelper 9 { 10 time_t expiration; 11 uint32_t flags; 12 memcached_st *memc; 13 memcached_return rc; 14 memcached_server_st *server; 15 public: 16 MemcachedHelper() 17 { 18 memc = memcached_create(NULL); 19 server = memcached_server_list_append(NULL,"10.10.10.233",11211,&rc); 20 rc=memcached_server_push(memc,server); 21 memcached_server_list_free(server); 22 expiration = 100; 23 } 24 ~MemcachedHelper() 25 { 26 memcached_free(memc); 27 } 28 public: 29 template<class T> void set(string in_key,vector<T> in_vect) 30 { 31 char* valueChar = new char[sizeof(T)* in_vect.size()]; 32 memcpy(valueChar, &in_vect[0], sizeof(T)* in_vect.size()); 33 size_t value_length = sizeof(T)* in_vect.size(); 34 rc=memcached_set(memc,in_key.c_str(),in_key.length(),valueChar,value_length,expiration,flags); 35 if(rc==MEMCACHED_SUCCESS) 36 { 37 cout<<"Set: "<<in_key<<" OK!"<<endl; 38 } 39 delete valueChar; 40 } 41 public: 42 template<class T> void get(string in_key,vector<T>& in_vect) 43 { 44 size_t value_length = 0; 45 size_t key_length = in_key.length(); 46 char* result = memcached_get(memc,in_key.c_str(), key_length,&value_length,&flags,&rc); 47 if(rc == MEMCACHED_SUCCESS) 48 { 49 in_vect.reserve(value_length / (sizeof(T))); 50 memcpy(&in_vect[0], result, value_length); 51 cout<<"Get: "<<in_key<<" OK!"<<endl; 52 } 53 } 54 }; 55 56 57 class Product 58 { 59 public: 60 Product(){} 61 Product(int in_id, int in_price) 62 { 63 id = in_id; 64 price = in_price; 65 } 66 67 public: 68 int id; 69 int price; 70 public: 71 int getid(){return id;} 72 int getprice(){return price;} 73 }; 74 75 76 int main() 77 { 78 Product product1(1,1); 79 Product product2(2,2); 80 Product product3(30,30); 81 vector<Product> product; 82 product.push_back(product1); 83 product.push_back(product2); 84 product.push_back(product3); 85 86 MemcachedHelper mem; 87 88 mem.set("oo",product); 89 90 vector<Product> result; 91 mem.get("oo",result); 92 cout<<result[2].id<<endl; 93 }
改进版(仍有待改进):
View Code
1 #include <iostream> 2 #include <string> 3 #include <libmemcached/memcached.h> 4 #include <vector> 5 6 #pragma once 7 8 using namespace std; 9 10 11 class MemcachedHelper 12 { 13 time_t expiration; 14 uint32_t flags; 15 memcached_st *memc; 16 memcached_return rc; 17 memcached_server_st *server; 18 public: 19 MemcachedHelper() 20 { 21 memc = memcached_create(NULL); 22 server = memcached_server_list_append(NULL,"10.10.10.233",11211,&rc); 23 rc=memcached_server_push(memc,server); 24 memcached_server_list_free(server); 25 expiration = 1000000; 26 } 27 ~MemcachedHelper() 28 { 29 memcached_free(memc); 30 } 31 32 public: 33 int set(string in_key,string in_value) 34 { 35 rc=memcached_set(memc,in_key.c_str(),in_key.length(),in_value.c_str(),in_value.length(),expiration,flags); 36 if(rc==MEMCACHED_SUCCESS) 37 return 1; 38 else 39 return 0; 40 } 41 42 public: 43 template<class T> int set(string in_key,vector<T> &in_vect) 44 { 45 char* valueChar = new char[sizeof(T)* in_vect.size()]; 46 memcpy(valueChar, &in_vect[0], sizeof(T)* in_vect.size()); 47 size_t value_length = sizeof(T)* in_vect.size(); 48 rc=memcached_set(memc,in_key.c_str(),in_key.length(),valueChar,value_length,expiration,flags); 49 if(rc==MEMCACHED_SUCCESS) 50 return 1; 51 else 52 return 0; 53 delete valueChar; 54 } 55 56 public: 57 int get(string in_key,string &out_value) 58 { 59 size_t value_length = 0; 60 char* result = memcached_get(memc,in_key.c_str(), in_key.length(),&value_length,&flags,&rc); 61 if(rc == MEMCACHED_SUCCESS) 62 { 63 out_value = result; 64 return 1; 65 } 66 else 67 { 68 return 0; 69 } 70 } 71 72 public: 73 template<class T> int get(string in_key,vector<T>& in_vect) 74 { 75 size_t value_length = 0; 76 size_t key_length = in_key.length(); 77 char* result = memcached_get(memc,in_key.c_str(), key_length,&value_length,&flags,&rc); 78 if(rc == MEMCACHED_SUCCESS) 79 { 80 in_vect.reserve(value_length / (sizeof(T))); 81 memcpy(&in_vect[0], result, value_length); 82 in_vect.assign(in_vect.begin(),in_vect.begin()+value_length/(sizeof(T))); 83 //cout<<"Get: "<<in_key<<" OK!"<<endl; 84 //size = value_length/sizeof(T); 85 return 1; 86 } 87 else 88 { 89 return 0; 90 } 91 } 92 };