redis 客户端编程

redis-hiredis 的安装

文章目录

      • [redis-hiredis 的安装](#redis-hiredis 的安装)
      • [核心 API 说明:](#核心 API 说明:)
      • 测试代码

下载地址:https://github.com/redis/hiredis

解压压缩包:

shell 复制代码
unzip redis-hiredis-v0.13.3-52-g5f98e1d.zip 

进入解压后的目录,编译源码:make

shell 复制代码
make

安装:

shell 复制代码
sudo make install 

在源码包目录下,有一个 examples 的目录,里面放了一些客户端示例代码:

核心 API 说明:

连接redis数据库

cpp 复制代码
redisContext *redisConnect(const char *ip, int port);
功能:连接redis数据库
参数:
ip:ip地址
port:端口,一般redis数据库的端口为6379
返回值:
成功:连接句柄redisContext
失败:NULL

执行的是redis数据库中的操作命令

cpp 复制代码
void *redisCommand(redisContext *c, const char *format, ...);
功能:执行的是redis数据库中的操作命令
参数:
c:连接数据库时 返回的redisContext
剩下的参数:剩下的参数为变参, 就如C标准函数printf函数一样的变参
返回值:
成功:void*, 一般强制转换成为redisReply类型的进行进一步的处理
失败:NULL
shell 复制代码
/* This is the reply object returned by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
size_t len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
状态标识 含义
REDIS_REPLY_STATUS 表示状态,内容通过 str 字段查看,字符串长度是 len 字段
REDIS_REPLY_ERROR 表示出错,查看出错信息,如上的 str,len 字段
REDIS_REPLY_INTEGER 返回整数,从 integer 字段获取值
REDIS_REPLY_NIL 没有数据返回
REDIS_REPLY_STRING 返回字符串,查看 str,len 字段
REDIS_REPLY_ARRAY elementindex 的方式访问数组元素,每个数组元素是一个 redisReply 对象的指针。
cpp 复制代码
void freeReplyObject(void *reply);
功能:释放redisCommand执行后返回的redisReply所占用的内存
参数:redisCommand执行后返回的redisReply
返回值:无

redisReply 返回结果处理,错误类型可通过 redisReply 的 errstr 字段查看简短的描述:

状态标识 含义
REDIS_OK 正常
REDIS_ERR_IO IO 读/写出现异常,通过 errno 查看原因
REDIS_ERR_EOF 服务器关闭了链接,读结束
REDIS_ERR_PROTOCOL 分析 redis 协议内容出错
EDIS_ERR_OTHER 其他未知的错误
cpp 复制代码
void redisFree(redisContext *c);
功能:释放redisConnect()所产生的连接
参数:redisConnect()所产生的连接
返回值:无

测试代码

c 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>

int main()
{
	redisContext *c = NULL;

	//连接到Redis数据库
	c = redisConnect("127.0.0.1", 6379);
	if (NULL == c || c->err != 0)
	{
		printf("RedisConnect failed: %s\n", c->errstr);
		return 1;
	}
	printf("连接到Redis成功\n");

    //执行Redis命令
	reply = redisCommand(c, "PING");
	if (NULL == reply)
	{
		printf("执行命令失败....\n");
		return 1;
	}

	if (reply->type == REDIS_REPLY_STATUS)
	{
		printf("执行结果: %s\n", reply->str);
	}

	//断开连接和释放内存
	redisFree(c);
    
	return 0;
}

执行set命令

c 复制代码
int main()
{
	redisContext *c = NULL;
	redisReply *reply = NULL;

	//连接到Redis数据库
	c = redisConnect("127.0.0.1", 6379);
	if (NULL == c || c->err != 0)
	{
		printf("RedisConnect failed: %s\n", c->errstr);
		return 1;
	}
	printf("连接到Redis成功\n");


	//执行Redis命令
	//reply = redisCommand(c, "set var wuyou");
	//reply = redisCommand(c, "set var %s", "hello wuyou");
	//reply = redisCommand(c, "set %s %s", "var", "hello wuyou");

	//执行set bar "hello" 后面的数字表示前面key和value的字节数
	reply = redisCommand(c, "set %b %b", "bar", (size_t)3, "hello", (size_t)5);
	if (NULL == reply)
	{
		printf("执行命令失败....\n");
		return 1;
	}

	if (reply->type == REDIS_REPLY_STATUS)
	{
		printf("执行结果: %s\n", reply->str);
	}

	//断开连接和释放内存
	redisFree(c);
	return 0;
}

Redis执行incr命令

c 复制代码
int main()
{
	/*前面都要先保证连接数据库成功*/
    
	//执行set bar "hello" 后面的数字表示前面key和value的字节数
	reply = redisCommand(c, "incr num");
	if (NULL == reply)
	{
		printf("执行命令失败....\n");
		return 1;
	}

	if (reply->type == REDIS_REPLY_INTEGER)
	{
		printf("执行结果: %lld\n", reply->integer);
	}

	//断开连接和释放内存
	redisFree(c);

	return 0;
}

Redis执行lpush和lrem命令

c 复制代码
int main()
{
	
	//删除
	reply = redisCommand(c, "del list");
	if (NULL == reply)
	{
		printf("执行命令失败....\n");
		return 1;
	}

	if (reply->type == REDIS_REPLY_INTEGER)
	{
		printf("执行结果: %lld\n", reply->integer);
	}
	//释放响应对象
	freeReplyObject(reply);
	//lpush命令
	for (int i = 0; i < 10; i++)
	{
		char buf[32];
		snprintf(buf, 64, "lpush list %u", i);
		reply = redisCommand(c, buf);
		//释放响应对象
		freeReplyObject(reply);
	}

	//执行lrange命令
	reply = redisCommand(c, "lrange list 0 -1");
	if (reply->type == REDIS_REPLY_ARRAY)
	{
		for (int i = 0; i < reply->elements; i++)
		{
			printf("%u) %s\n", i, reply->element[i]->str);
		}
	}
	//释放响应对象
	freeReplyObject(reply);

	//断开连接和释放内存
	redisFree(c);
	return 0;
}
相关推荐
Decay__1 小时前
SQL Server 数据库全栈运维实战系列 — 合集索引(24 篇)
运维·数据库·sql·mysql·架构
Thanks_ks2 小时前
SQL 的 GROUP BY 与聚合函数:从会写 SELECT 到会做统计
数据库·sql·mysql·group by·数据统计·聚合函数·having
IT规划师2 小时前
Redis 数据类型全解 - Stream 深度:消费者组、Pending 队列与死信处理(十)
redis·.net
Decay__10 小时前
12_MySQL InnoDB 内存与锁调优 —— Buffer Pool、redo log、隔离级别与锁等待
运维·数据库·sql·mysql·架构
笃行35010 小时前
AI数据库的库内四层结构,KingbaseES 产品拆解
数据库
我是大猴子12 小时前
redis
数据库·redis·缓存
苏宸啊14 小时前
MySQL(一)
数据库·mysql
2603_9696948515 小时前
JDBC 基础学习笔记:API 核心原理解析与 CRUD 代码实操
数据库·笔记·mysql·学习方法·database·sql注入
这个DBA有点耶15 小时前
库内语义构建 vs 外挂向量库:业务语义缺失,才是RAG“胡说八道”的根源
数据库·mysql·aigc
kobe_OKOK_16 小时前
mysql 开启 binlog 日志
数据库·mysql