Sword redis存取二进制数据
#include "hiredis/hiredis.h" /* redis头文件 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <string> //初始化 int init(const char *ip,uint16_t port) { redisContext *_context; //创建redis链接 _context = redisConnect(ip, port); if (NULL == _context) { return -1; } return 0; } //二进制数据set操作 /******************************************************** Func Name: getInstance Date Created: 2018-10-12 Description: 创建实例对象 Input:@key: key值 @value: value值 @vlen: value数据长度 Output: Return: error code Caution: *********************************************************/ int set(redisContext *_context, const char *key, uint8_t *value, uint32_t vlen) { /* 为什么argv数组长度是5 set key value ex time 元素1 元素2 元素3 元素4 元素5 */ const char * argv[5] = { 0 }; //用来存储所有的数据 size_t argvlen[5] = { 0 }; //用来存储数据的长度 redisReply *rep = NULL; std::string strRes; if (NULL == key || NULL == value || 0 == vlen) { return -1; } argv[0] = "set"; argvlen[0] = strlen("set"); argv[1] = key; argvlen[1] = strlen(key); argv[2] = (char *)value; argvlen[2] = vlen; //设置超时时间 argv[3] = "ex"; argvlen[3] = strlen("ex"); argv[4] = "3600"; argvlen[4] = strlen("3600"); rep = (redisReply *)redisCommandArgv(_context, 5, argv, argvlen); if (NULL == rep) { return -1; } if (REDIS_REPLY_STATUS == rep->type) { strRes = rep->str; } freeReplyObject(rep); rep = NULL; return ("OK" == strRes ? 0 : -1); } /******************************************************** Func Name: get Date Created: 2018-12-11 Description: get Input: Output: Return: error code Caution: *********************************************************/ int get(redisContext *_context, const char *key, uint8_t *&value, uint32_t &vlen) { redisReply *rep = NULL; char *cmd = NULL; if (NULL == key) { return -1; } cmd = (char *)malloc(strlen(key) + strlen("get") + 10); if (NULL == cmd) { return -1; } memset(cmd, 0, strlen(key) + strlen("get") + 10); sprintf(cmd, "get %s", key); rep = (redisReply *)redisCommand(_context, cmd); if (NULL == rep) { return -1; } vlen = rep->len; if (rep->len <= 0) { return -1; } value = (uint8_t *)malloc(vlen); if (NULL == value) { return -1; } memset(value, 0, vlen); memcpy(value, rep->str, rep->len); freeReplyObject(rep); rep = NULL; return 0; }
问题:客户端无法登录Redis服务器报错,解除保护模式 解决方案 1、修改redis服务器的配置文件 vi redis.conf 注释以下绑定的主机地址 # bind 127.0.0.1 2、修改redis服务器的参数配置 修改redis的守护进程为no ,不启用 127.0.0.1:6379> config set daemonize "no" OK 修改redis的保护模式为no,不启用 127.0.0.1:6379> config set protected-mode "no" 注意:修改redis服务器的参数配置,只能通过redis-cli客户端修改(直接修改redis.conf没有效果), 如果redis服务器重启了,那么修改将会失效