1. linux下安装及配置

1.1 编译

  1. $ tar zxvf redis-4.0.10.tar.gz
  2. $ cd redis-4.0.10
  3. $ make

1.2 安装hiredis (c++需要的库)

  1. $ cd redis-4.0.10/deps/hiredis
  2. $ sudo make install

1.3 配置redis (redis.conf)

  1. $ cd redis-4.0.10/
  2. $ vim redis.conf

  1. # 允许网络访问
  2. # bind 127.0.0.1
  3. # 关闭远程连接的保护模式
  4. protected-mode no
  5. # 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes。
  6. # 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面。
  7. daemonize yes
  8. # 当redis作为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,
  9. # 但是你可以在这里自己制定它的文件位置。
  10. pidfile /var/run/redis.pid
  11. # 日志文件级别,默认notice。从低到高分别是debug, verbose,notice,warning
  12. loglevel notice
  13. # 日志文件路径
  14. logfile "/data/redis_file/redis.log"
  15. # 因为我们对数据一致性与完成性要求不高,所以使用RDB缓存模式
  16. ################################ 快照 ################################
  17. #
  18. # 存 DB 到磁盘:
  19. #
  20. # 格式:save <间隔时间(秒)> <写入次数>
  21. #
  22. # 根据给定的时间间隔和写入次数将数据保存到磁盘
  23. #
  24. # 下面的例子的意思是:
  25. # 900 秒内如果至少有 1 个 key 的值变化,则保存
  26. # 300 秒内如果至少有 10 个 key 的值变化,则保存
  27. # 60 秒内如果至少有 10000 个 key 的值变化,则保存
  28. #  
  29. # 注意:你可以注释掉所有的 save 行来停用保存功能。
  30. # 也可以直接一个空字符串来实现停用:
  31. # save ""
  32. save 900 1
  33. save 300 10
  34. save 60 10000
  35. # 设置 dump 的文件位置
  36. dbfilename dump.rdb
  37. # 工作目录
  38. # 例如上面的 dbfilename 只指定了文件名,
  39. # 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。
  40. dir /data/redis_file
  41. # 最大使用内存,这里是2G,单位是bytes
  42. # maxmemory 2147483648
  43. # 达到最大内存使用的策略,默认是noeviction
  44. # volatile-lru -> 根据LRU算法生成的过期时间来删除。
  45. # allkeys-lru -> 根据LRU算法删除任何key。
  46. # volatile-lfu -> 根据LFU算法生成的过期时间来删除。
  47. # allkeys-lfu -> 根据LFU算法删除任何key。
  48. # volatile-random -> 根据过期设置来随机删除key。
  49. # allkeys->random -> 无差别随机删。
  50. # volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
  51. # noeviction -> 谁也不删,直接在写操作时返回错误。
  52. maxmemory-policy allkeys->random
  53. # 不使用AOF缓存模式
  54. appendonly no
  55. appendfilename "appendonly.aof"
  56. appendfsync everysec
  57. # 设置密码
  58. requirepass mdsoss
  59. # 其他默认即可

1.4 启动redis

  1. $ cd redis-4.0.10/src
  2. $ ./redis-server ../redis.conf

1.5 redis测试程序

在C++中用hiredis.h时,只需要
#include 这样引入即可

  • 测试代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stddef.h>
  4. #include <stdarg.h>
  5. #include <assert.h>
  6. #include <hiredis/hiredis.h> //redis C接口库
  7. #define REDIS_HOST "127.0.0.1"
  8. #define REDIS_PORT 6379
  9. redisContext *g_ctx = NULL;
  10. int redis_init()
  11. {
  12. redisContext *c = NULL;
  13. c = redisConnect(REDIS_HOST, REDIS_PORT);
  14. if (NULL == c || c->err) {
  15. if(c) {
  16. printf("Redis [%s:%d], Error:[%s]\n", REDIS_HOST, REDIS_PORT, c->errstr);
  17. redisFree(c);
  18. } else {
  19. printf("Redis [%s:%d] failure\n", REDIS_HOST, REDIS_PORT);
  20. }
  21. return -1;
  22. }
  23. g_ctx = c;
  24. return 0;
  25. }
  26. void redis_free()
  27. {
  28. if (g_ctx) {
  29. redisFree(g_ctx);
  30. }
  31. g_ctx = NULL;
  32. }
  33. int redis_test(const char *cmd)
  34. {
  35. int i = 0;
  36. redisReply *r = NULL;
  37. if (NULL == cmd) {
  38. return -1;
  39. }
  40. printf("%s\n", cmd);
  41. r = (redisReply *)redisCommand(g_ctx, cmd);
  42. if (NULL == r) {
  43. printf("%s, Error[%d:%s]", g_ctx->err, g_ctx->errstr);
  44. return -1;
  45. }
  46. printf("type: %d\n", r->type);
  47. switch(r->type) {
  48. case REDIS_REPLY_STATUS:
  49. printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);
  50. break;
  51. case REDIS_REPLY_ERROR:
  52. printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);
  53. break;
  54. case REDIS_REPLY_INTEGER:
  55. printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);
  56. break;
  57. case REDIS_REPLY_NIL:
  58. printf("type:%s, no data\n", "REDIS_REPLY_NIL");
  59. break;
  60. case REDIS_REPLY_STRING:
  61. printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);
  62. break;
  63. case REDIS_REPLY_ARRAY:
  64. printf("type:%s, reply->elements:%d\n", "REDIS_REPLY_ARRAY", r->elements);
  65. for (i = 0; i < r->elements; i++) {
  66. printf("%d: %s\n", i, r->element[i]->str);
  67. }
  68. break;
  69. default:
  70. printf("unkonwn type:%s\n", r->type);
  71. break;
  72. }
  73. /*release reply and context */
  74. freeReplyObject(r);
  75. return 0;
  76. }
  77. int main()
  78. {
  79. const char *cmd = NULL;
  80. /* init redis */
  81. if (redis_init()) {
  82. return -1;
  83. }
  84. /* 1: SET<--->GET */
  85. printf("SET<--->GET\n");
  86. cmd = "set foo bar";
  87. redis_test(cmd);
  88. cmd = "get foo";
  89. redis_test(cmd);
  90. /* 2: SADD<--->SMEMBERS */
  91. printf("\nSADD<--->SMEMBERS\n");
  92. cmd = "SADD namelist jack lily lucy tom";
  93. redis_test(cmd);
  94. cmd = "SMEMBERS namelist";
  95. redis_test(cmd);
  96. /* 3: .....*/
  97. /* free redis context */
  98. redis_free();
  99. return 0;
  100. }
  • 编译: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

  1. <a name="wRsdZ"></a>
  2. ## 1.6 redis命令
  3. - 查看redis信息

$ cd redis-4.0.10/src $ ./redis-cli $ info

  1. - 结果显示(内存部分):

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