高速缓存系统之memcache c++使用实例

下载源码编译,memcached就是生成的主程序,启动可指定端口,memcached作为server端,依然是我们熟悉的cs模式,使用两个client一个setkey,一个getkey一百万个做测试。

[cpp]  view plain  copy
  1. ./memcached -d -m 300 -p 11211 -u root -c 4096  
[cpp]  view plain  copy
  1. #include "../libmemcached-1.0.7/libmemcached/memcached.h"  
  2. #include<iostream>  
  3. #include<string>  
  4. #include<time.h>  
  5. using std::string;  
  6. using std::cout;  
  7. using std::endl;  
  8.   
  9. class MemCachedClient  
  10. {  
  11.     public:  
  12.         ~MemCachedClient()  
  13.         {  
  14.             memcached_free(memc);  
  15.         };  
  16.   
  17.         MemCachedClient()  
  18.         {  
  19.             memcached_return rc;  
  20.             memcached_server_st *server = NULL;  
  21.   
  22.             memc = memcached_create(NULL);  
  23.   
  24.             server =memcached_server_list_append(server, "127.0.0.1", 11211, &rc);  
  25.   
  26.             rc=memcached_server_push(memc,server);  
  27.   
  28.             if (MEMCACHED_SUCCESS != rc)  
  29.             {  
  30.                 cout <<"memcached_server_push failed! rc: " << rc << endl;  
  31.             }  
  32.   
  33.             memcached_server_list_free(server);  
  34.         };  
  35.   
  36.   
  37.         int Insert(const char* key, const char* value,time_t expiration = 3)  
  38.         {  
  39.             if (NULL == key || NULL == value)  
  40.             {  
  41.                 return -1;  
  42.             }  
  43.   
  44.             uint32_t flags = 0;  
  45.   
  46.             memcached_return rc;  
  47.   
  48.             rc = memcached_set(memc, key, strlen(key),value, strlen(value)+1, expiration, flags);  
  49.   
  50.             if (MEMCACHED_SUCCESS == rc)  
  51.             {  
  52.                 return 1;  
  53.             }  
  54.             else  
  55.             {  
  56.                 return 0;  
  57.             }  
  58.         };  
  59.   
  60.   
  61.         string Get(const char* key)  
  62.         {  
  63.             if (NULL == key)  
  64.             {  
  65.                 return "";  
  66.             }  
  67.   
  68.             uint32_t flags = 0;  
  69.   
  70.             memcached_return rc;  
  71.   
  72.             size_t value_length;  
  73.             char* value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);  
  74.   
  75.             if(rc == MEMCACHED_SUCCESS)  
  76.             {  
  77.                 return value;  
  78.             }  
  79.   
  80.             return "";  
  81.         };  
  82.   
  83.     private:  
  84.         memcached_st* memc;  
  85. };  
  86.   
  87. #include<iostream>  
  88. using std::cout;  
  89. using std::endl;  
  90. #include<time.h>  
  91. #include<stdlib.h>  
  92. #include<stdio.h>  
  93.   
  94. int main()  
  95. {  
  96.     MemCachedClient mc;  
  97.     clock_t begin_tick_counts = clock();  
  98.     long object_nums = 1000000;  
  99.     char buff[32];  
  100.     for (int i=0; i<=object_nums; ++i)  
  101.     {  
  102.         sprintf(buff, "key-%d",i);  
  103.         mc.Insert(buff,buff, 60);  
  104.         //printf("%s\n", mc.Get(buff).c_str());  
  105.     }  
  106.     clock_t end_tick_counts = clock();  
  107.     printf("set %ld values costs %f ticks\n", object_nums, (double)(end_tick_counts - begin_tick_counts)/CLOCKS_PER_SEC);  
  108.     return 1;  
  109. }  

[cpp]  view plain  copy
  1. #include "../libmemcached-1.0.7/libmemcached/memcached.h"  
  2. #include<iostream>  
  3. #include<string>  
  4. #include<time.h>  
  5. using std::string;  
  6. using std::cout;  
  7. using std::endl;  
  8.   
  9. class MemCachedClient  
  10. {  
  11.     public:  
  12.         ~MemCachedClient()  
  13.         {  
  14.             memcached_free(memc);  
  15.         };  
  16.   
  17.         MemCachedClient()  
  18.         {  
  19.             memcached_return rc;  
  20.             memcached_server_st *server = NULL;  
  21.   
  22.             memc = memcached_create(NULL);  
  23.   
  24.             server =memcached_server_list_append(server, "127.0.0.1", 11211, &rc);  
  25.   
  26.             rc=memcached_server_push(memc,server);  
  27.   
  28.             if (MEMCACHED_SUCCESS != rc)  
  29.             {  
  30.                 cout <<"memcached_server_push failed! rc: " << rc << endl;  
  31.             }  
  32.   
  33.             memcached_server_list_free(server);  
  34.         };  
  35.   
  36.   
  37.         int Insert(const char* key, const char* value,time_t expiration = 3)  
  38.         {  
  39.             if (NULL == key || NULL == value)  
  40.             {  
  41.                 return -1;  
  42.             }  
  43.   
  44.             uint32_t flags = 0;  
  45.   
  46.             memcached_return rc;  
  47.   
  48.             rc = memcached_set(memc, key, strlen(key),value, strlen(value)+1, expiration, flags);  
  49.   
  50.             if (MEMCACHED_SUCCESS == rc)  
  51.             {  
  52.                 return 1;  
  53.             }  
  54.             else  
  55.             {  
  56.                 return 0;  
  57.             }  
  58.         };  
  59.   
  60.   
  61.         string Get(const char* key)  
  62.         {  
  63.             if (NULL == key)  
  64.             {  
  65.                 return "";  
  66.             }  
  67.   
  68.             uint32_t flags = 0;  
  69.   
  70.             memcached_return rc;  
  71.   
  72.             size_t value_length;  
  73.             char* value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);  
  74.   
  75.             if(rc == MEMCACHED_SUCCESS)  
  76.             {  
  77.                 return value;  
  78.             }  
  79.   
  80.             return "";  
  81.         };  
  82.   
  83.     private:  
  84.         memcached_st* memc;  
  85. };  
  86.   
  87. #include<iostream>  
  88. using std::cout;  
  89. using std::endl;  
  90. #include<time.h>  
  91. #include<stdlib.h>  
  92. #include<stdio.h>  
  93.   
  94. int main()  
  95. {  
  96.     MemCachedClient mc;  
  97.     clock_t begin_tick_counts = clock();  
  98.     long object_nums = 1000000;  
  99.     char buff[32];  
  100.     for (int i=0; i<=object_nums; ++i)  
  101.     {  
  102.         sprintf(buff, "key-%d",i);  
  103.         const char *key = mc.Get(buff).c_str();  
  104.         if(i == object_nums)  
  105.             printf("key:%s\n", key);  
  106.     }  
  107.     clock_t end_tick_counts = clock();  
  108.     printf("get %ld value costs:%f ticks\n", object_nums, (double)(end_tick_counts - begin_tick_counts)/CLOCKS_PER_SEC);  
  109.     return 1;  
  110. }  

[cpp]  view plain  copy
  1. TARGET = test_memcached_getkey  
  2. SOURCE = test_memcached_getkey.cpp  
  3. CFLAG  = -g3 -O0 -Wl,-rpath=./ -lmemcached  
  4.   
  5. TARGET:  
  6.     g++ $(SOURCE) -o $(TARGET) $(CFLAG)  
  7. test_memcached_setkey:  
  8.     g++ test_memcached_setkey.cpp -o test_memcached_setkey $(CFLAG)  
  9. .phony:clean  
  10. clean:  
  11.     rm $(TARGET)  
  12.     rm test_memcached_setkey  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值