高速缓存系统之memcache c++使用实例
2017-10-18 17:23
477 查看
下载源码编译,memcached就是生成的主程序,启动可指定端口,memcached作为server端,依然是我们熟悉的cs模式,使用两个client一个setkey,一个getkey一百万个做测试。
./memcached -d -m 300 -p 11211 -u root -c 4096
./memcached -d -m 300 -p 11211 -u root -c 4096
#include "../libmemcached-1.0.7/libmemcached/memcached.h" #include<iostream> #include<string> #include<time.h> using std::string; using std::cout; using std::endl; class MemCachedClient { public: ~MemCachedClient() { memcached_free(memc); }; MemCachedClient() { memcached_return rc; memcached_server_st *server = NULL; memc = memcached_create(NULL); server =memcached_server_list_append(server, "127.0.0.1", 11211, &rc); rc=memcached_server_push(memc,server); if (MEMCACHED_SUCCESS != rc) { cout <<"memcached_server_push failed! rc: " << rc << endl; } memcached_server_list_free(server); }; int Insert(const char* key, const char* value,time_t expiration = 3) { if (NULL == key || NULL == value) { return -1; } uint32_t flags = 0; memcached_return rc; rc = memcached_set(memc, key, strlen(key),value, strlen(value)+1, expiration, flags); if (MEMCACHED_SUCCESS == rc) { return 1; } else { return 0; } }; string Get(const char* key) { if (NULL == key) { return ""; } uint32_t flags = 0; memcached_return rc; size_t value_length; char* value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc); if(rc == MEMCACHED_SUCCESS) { return value; } return ""; }; private: memcached_st* memc; }; #include<iostream> using std::cout; using std::endl; #include<time.h> #include<stdlib.h> #include<stdio.h> int main() { MemCachedClient mc; clock_t begin_tick_counts = clock(); long object_nums = 1000000; char buff[32]; for (int i=0; i<=object_nums; ++i) { sprintf(buff, "key-%d",i); mc.Insert(buff,buff, 60); //printf("%s\n", mc.Get(buff).c_str()); } clock_t end_tick_counts = clock(); printf("set %ld values costs %f ticks\n", object_nums, (double)(end_tick_counts - begin_tick_counts)/CLOCKS_PER_SEC); return 1; }
#include "../libmemcached-1.0.7/libmemcached/memcached.h" #include<iostream> #include<string> #include<time.h> using std::string; using std::cout; using std::endl; class MemCachedClient { public: ~MemCachedClient() { memcached_free(memc); }; MemCachedClient() { memcached_return rc; memcached_server_st *server = NULL; memc = memcached_create(NULL); server =memcached_server_list_append(server, "127.0.0.1", 11211, &rc); rc=memcached_server_push(memc,server); if (MEMCACHED_SUCCESS != rc) { cout <<"memcached_server_push failed! rc: " << rc << endl; } memcached_server_list_free(server); }; int Insert(const char* key, const char* value,time_t expiration = 3) { if (NULL == key || NULL == value) { return -1; } uint32_t flags = 0; memcached_return rc; rc = memcached_set(memc, key, strlen(key),value, strlen(value)+1, expiration, flags); if (MEMCACHED_SUCCESS == rc) { return 1; } else { return 0; } }; string Get(const char* key) { if (NULL == key) { return ""; } uint32_t flags = 0; memcached_return rc; size_t value_length; char* value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc); if(rc == MEMCACHED_SUCCESS) { return value; } return ""; }; private: memcached_st* memc; }; #include<iostream> using std::cout; using std::endl; #include<time.h> #include<stdlib.h> #include<stdio.h> int main() { MemCachedClient mc; clock_t begin_tick_counts = clock(); long object_nums = 1000000; char buff[32]; for (int i=0; i<=object_nums; ++i) { sprintf(buff, "key-%d",i); const char *key = mc.Get(buff).c_str(); if(i == object_nums) printf("key:%s\n", key); } clock_t end_tick_counts = clock(); printf("get %ld value costs:%f ticks\n", object_nums, (double)(end_tick_counts - begin_tick_counts)/CLOCKS_PER_SEC); return 1; }
TARGET = test_memcached_getkey SOURCE = test_memcached_getkey.cpp CFLAG = -g3 -O0 -Wl,-rpath=./ -lmemcached TARGET: g++ $(SOURCE) -o $(TARGET) $(CFLAG) test_memcached_setkey: g++ test_memcached_setkey.cpp -o test_memcached_setkey $(CFLAG) .phony:clean clean: rm $(TARGET) rm test_memcached_setkey
相关文章推荐
- 高速缓存系统之redis c++使用实例
- Java中读写锁的实现及使用读写锁简单实现缓存系统的实例
- 缓存系统memcache的安装,配置和使用
- 在window系统下安装memcache内在缓存的实例
- (wdlinux)缓存系统memcache的安装,配置和使用
- DotNET企业架构应用实践-系统架构与性能-在业务中实例使用缓存与缓存查询-附上视频
- 【系统架构】缓存Memcache 使用原子性操作add,实现并发锁
- ruby系统使用memcache缓存
- DotNET企业架构应用实践-系统架构与性能-在业务中实例使用缓存与缓存查询-附上视频
- java中读写锁的实现及使用读写锁简单实现缓存系统的实例
- 缓存系统memcache的安装,配置和使用
- 缓存系统memcache的安装,配置和使用
- java中读写锁的实现及使用读写锁简单实现缓存系统的实例
- php 如何使用 缓存MEMCACHE 实例
- c++中的引用的使用原理和使用实例 (3)
- Java写的手机计费系统(作为学习参考的实例:关键是Java类库里面的date和Calendar的使用和相互转换,以及一些日期的实际问题)
- FAT32文件系统中使用VS2005调试C++项目出现MSVCP80D.dll错误
- ASP.NET使用Memcached高缓存实例
- 下面是在NHibernate中使用memcache二级缓存的详细操作记录。
- 使用Mutex实现会话状态下单实例运行和系统范围内单实例运行