#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 -;
} return ;
} //二进制数据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[] = { }; //用来存储所有的数据
size_t argvlen[] = { }; //用来存储数据的长度
redisReply *rep = NULL;
std::string strRes; if (NULL == key || NULL == value || == vlen)
{
return -;
} argv[] = "set";
argvlen[] = strlen("set"); argv[] = key;
argvlen[] = strlen(key); argv[] = (char *)value;
argvlen[] = vlen; //设置超时时间 argv[] = "ex";
argvlen[] = strlen("ex"); argv[] = "";
argvlen[] = strlen(""); rep = (redisReply *)redisCommandArgv(_context, , argv, argvlen);
if (NULL == rep)
{
return -;
} if (REDIS_REPLY_STATUS == rep->type)
{
strRes = rep->str;
} freeReplyObject(rep);
rep = NULL; return ("OK" == strRes ? : -); } /********************************************************
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 -;
} cmd = (char *)malloc(strlen(key) + strlen("get") + );
if (NULL == cmd)
{
return -;
}
memset(cmd, , strlen(key) + strlen("get") + );
sprintf(cmd, "get %s", key); rep = (redisReply *)redisCommand(_context, cmd);
if (NULL == rep)
{
return -;
} vlen = rep->len;
if (rep->len <= )
{
return -;
}
value = (uint8_t *)malloc(vlen);
if (NULL == value)
{
return -;
}
memset(value, , vlen);
memcpy(value, rep->str, rep->len); freeReplyObject(rep);
rep = NULL; return ;
}
问题:客户端无法登录Redis服务器报错,解除保护模式 解决方案
、修改redis服务器的配置文件
vi redis.conf 注释以下绑定的主机地址
# bind 127.0.0.1 、修改redis服务器的参数配置 修改redis的守护进程为no ,不启用
127.0.0.1:> config set daemonize "no"
OK 修改redis的保护模式为no,不启用
127.0.0.1:> config set protected-mode "no" 注意:修改redis服务器的参数配置,只能通过redis-cli客户端修改(直接修改redis.conf没有效果),
如果redis服务器重启了,那么修改将会失效