1. linux下安装及配置
1.1 编译
$ tar zxvf redis-4.0.10.tar.gz$ cd redis-4.0.10$ make
1.2 安装hiredis (c++需要的库)
$ cd redis-4.0.10/deps/hiredis$ sudo make install
1.3 配置redis (redis.conf)
$ cd redis-4.0.10/$ vim redis.conf
# 允许网络访问# bind 127.0.0.1# 关闭远程连接的保护模式protected-mode no# 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes。# 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面。daemonize yes# 当redis作为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,# 但是你可以在这里自己制定它的文件位置。pidfile /var/run/redis.pid# 日志文件级别,默认notice。从低到高分别是debug, verbose,notice,warningloglevel notice# 日志文件路径logfile "/data/redis_file/redis.log"# 因为我们对数据一致性与完成性要求不高,所以使用RDB缓存模式################################ 快照 ################################## 存 DB 到磁盘:## 格式:save <间隔时间(秒)> <写入次数>## 根据给定的时间间隔和写入次数将数据保存到磁盘## 下面的例子的意思是:# 900 秒内如果至少有 1 个 key 的值变化,则保存# 300 秒内如果至少有 10 个 key 的值变化,则保存# 60 秒内如果至少有 10000 个 key 的值变化,则保存## 注意:你可以注释掉所有的 save 行来停用保存功能。# 也可以直接一个空字符串来实现停用:# save ""save 900 1save 300 10save 60 10000# 设置 dump 的文件位置dbfilename dump.rdb# 工作目录# 例如上面的 dbfilename 只指定了文件名,# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。dir /data/redis_file# 最大使用内存,这里是2G,单位是bytes# maxmemory 2147483648# 达到最大内存使用的策略,默认是noeviction# volatile-lru -> 根据LRU算法生成的过期时间来删除。# allkeys-lru -> 根据LRU算法删除任何key。# volatile-lfu -> 根据LFU算法生成的过期时间来删除。# allkeys-lfu -> 根据LFU算法删除任何key。# volatile-random -> 根据过期设置来随机删除key。# allkeys->random -> 无差别随机删。# volatile-ttl -> 根据最近过期时间来删除(辅以TTL)# noeviction -> 谁也不删,直接在写操作时返回错误。maxmemory-policy allkeys->random# 不使用AOF缓存模式appendonly noappendfilename "appendonly.aof"appendfsync everysec# 设置密码requirepass mdsoss# 其他默认即可
1.4 启动redis
$ cd redis-4.0.10/src$ ./redis-server ../redis.conf
1.5 redis测试程序
在C++中用hiredis.h时,只需要
#include这样引入即可
- 测试代码:
#include <stdio.h>#include <string.h>#include <stddef.h>#include <stdarg.h>#include <assert.h>#include <hiredis/hiredis.h> //redis C接口库#define REDIS_HOST "127.0.0.1"#define REDIS_PORT 6379redisContext *g_ctx = NULL;int redis_init(){redisContext *c = NULL;c = redisConnect(REDIS_HOST, REDIS_PORT);if (NULL == c || c->err) {if(c) {printf("Redis [%s:%d], Error:[%s]\n", REDIS_HOST, REDIS_PORT, c->errstr);redisFree(c);} else {printf("Redis [%s:%d] failure\n", REDIS_HOST, REDIS_PORT);}return -1;}g_ctx = c;return 0;}void redis_free(){if (g_ctx) {redisFree(g_ctx);}g_ctx = NULL;}int redis_test(const char *cmd){int i = 0;redisReply *r = NULL;if (NULL == cmd) {return -1;}printf("%s\n", cmd);r = (redisReply *)redisCommand(g_ctx, cmd);if (NULL == r) {printf("%s, Error[%d:%s]", g_ctx->err, g_ctx->errstr);return -1;}printf("type: %d\n", r->type);switch(r->type) {case REDIS_REPLY_STATUS:printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);break;case REDIS_REPLY_ERROR:printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);break;case REDIS_REPLY_INTEGER:printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);break;case REDIS_REPLY_NIL:printf("type:%s, no data\n", "REDIS_REPLY_NIL");break;case REDIS_REPLY_STRING:printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);break;case REDIS_REPLY_ARRAY:printf("type:%s, reply->elements:%d\n", "REDIS_REPLY_ARRAY", r->elements);for (i = 0; i < r->elements; i++) {printf("%d: %s\n", i, r->element[i]->str);}break;default:printf("unkonwn type:%s\n", r->type);break;}/*release reply and context */freeReplyObject(r);return 0;}int main(){const char *cmd = NULL;/* init redis */if (redis_init()) {return -1;}/* 1: SET<--->GET */printf("SET<--->GET\n");cmd = "set foo bar";redis_test(cmd);cmd = "get foo";redis_test(cmd);/* 2: SADD<--->SMEMBERS */printf("\nSADD<--->SMEMBERS\n");cmd = "SADD namelist jack lily lucy tom";redis_test(cmd);cmd = "SMEMBERS namelist";redis_test(cmd);/* 3: .....*//* free redis context */redis_free();return 0;}
- 编译:g++ -lhiredis -o redis2 redis2.cpp
- 执行:./redis2
- 运行结果: ``` SET<—->GET set foo bar type: 5 type:REDIS_REPLY_STATUS, reply->len:2 reply->str:OK get foo type: 1 type:REDIS_REPLY_STRING, reply->len:3 reply->str:bar
SADD<—->SMEMBERS SADD namelist jack lily lucy tom type: 3 type:REDIS_REPLY_INTEGER, reply->integer:4 SMEMBERS namelist type: 2 type:REDIS_REPLY_ARRAY, reply->elements:4 0: lucy 1: lily 2: jack 3: tom
<a name="wRsdZ"></a>## 1.6 redis命令- 查看redis信息
$ cd redis-4.0.10/src $ ./redis-cli $ info
- 结果显示(内存部分):
Memory
used_memory:849856 used_memory_human:829.94K //数据占用了多少内存 used_memory_rss:8171520 used_memory_rss_human:7.79M //redis占用了多少内存 used_memory_peak:849856 used_memory_peak_human:829.94K used_memory_peak_perc:100.12% used_memory_overhead:836310 used_memory_startup:786496 used_memory_dataset:13546 used_memory_dataset_perc:21.38% total_system_memory:8391725056 total_system_memory_human:7.82G //系统内存 used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:9.61 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0 ```
1.7 性能对比
std::map与redis::hash对比插入和查找100w数据花费的时间。
是std::string,int
