redis系列-redis的连接

本文简要介绍了 Redis 客户端连接的基本方式和常用命令的使用方法,包括如何通过 hiredis 库进行连接尝试、错误处理以及执行基本的 Redis 命令,如设置键值对。实例演示了如何连接 Redis 服务器并执行 set 命令来存储数据。

转自:http://blog.youkuaiyun.com/whuqin/article/details/17530709

Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

redis客户端连接比较简单,但日常中redis的使用和维护会有很多地方需要学习。本文只简单介绍下常用函数。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //hiredis/hiredis.h  
  2. /* Context for a connection to Redis */  
  3. typedef struct redisContext {  
  4.     int err; /* Error flags, 0 when there is no error */  
  5.     char errstr[128]; /* String representation of error when applicable */  
  6.     int fd;   
  7.     int flags;  
  8.     char *obuf; /* Write buffer */  
  9.     redisReader *reader; /* Protocol reader */  
  10. } redisContext;  
  11.   
  12. /* This is the reply object returned by redisCommand() */  
  13. #define REDIS_REPLY_STRING 1  
  14. #define REDIS_REPLY_ARRAY 2  
  15. #define REDIS_REPLY_INTEGER 3  
  16. #define REDIS_REPLY_NIL 4  
  17. #define REDIS_REPLY_STATUS 5  
  18. #define REDIS_REPLY_ERROR 6  
  19. typedef struct redisReply {  
  20.     int type; /* REDIS_REPLY_* */  
  21.     long long integer; /* The integer when type is REDIS_REPLY_INTEGER */  
  22.     int len; /* Length of string */  
  23.     char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */  
  24.     size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */  
  25.     struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */  
  26. } redisReply;  
  27.   
  28. redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);  
  29. void redisFree(redisContext *c);  
  30. //Issue a command to Redis, NULL if error, otherwise reply  
  31. void *redisCommand(redisContext *c, const char *format, ...);  
  32. /* Function to free the reply objects hiredis returns by default. */  
  33. void freeReplyObject(void *reply);  


应用实例:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "hiredis/hiredis.h"  
  2.   redisContext* context = NULL;  
  3.   redisReply* reply = NULL;  
  4.   int retry = 0;  
  5.   while (retry < 5)   
  6.   {  
  7.     reply != NULL ? freeReplyObject(reply):;  
  8.     context != NULL ? redisFree(context):;  
  9.     context = redisConnect("127.0.0.1", 8600);  
  10.     if (NULL == context || context->err != 0)  
  11.     {  
  12.       cout << "connect err" << endl;  
  13.       continue;  
  14.     }  
  15.     redisReply* reply = (redisReply*)redisCommand(context, "%s""set name John");  
  16.     if (NULL == reply || reply == REDIS_REPLY_ERROR)  
  17.     {  
  18.      cout << "exe command fail" << endl;  
  19.      continue;  
  20.     }  
  21.     cout << "ok" << endl;    
  22.   }  
  23.   reply != NULL ? freeReplyObject(reply):;  
  24.   context != NULL ? redisFree(context):;  
Redis 提供了一系列工具和脚本以帮助用户更好地进行安装、测试、维护和性能评估。以下是对提到的工具和脚本的详细说明及其使用方式: ### `mkreleasehdr.sh` 脚本 `mkreleasehdr.sh` 是 Redis 源码构建过程中使用的一个脚本,主要负责生成版本头文件。它会根据 Git 仓库的信息生成 `version.h` 文件,该文件包含了当前 Redis 的版本号、Git 提交哈希值等元数据信息。这个脚本通常在 Redis 编译过程中自动运行,开发者无需手动调用它。如果手动需要运行,可以执行以下命令: ```bash ./mkreleasehdr.sh ``` 此脚本确保了 Redis 版本信息的动态生成,方便在不同环境中进行版本追踪和调试。 ### `redis-benchmark`:基准性能测试工具 `redis-benchmark` 是 Redis 自带的性能测试工具,用于评估 Redis 服务器的吞吐量、延迟等关键性能指标。它支持多种测试模式和选项,例如并发连接数、请求类型等。例如,测试本地 Redis 服务器的性能可以使用以下命令: ```bash redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 10000 ``` 其中 `-h` 指定服务器地址,`-p` 指定端口,`-c` 表示并发客户端数量,`-n` 表示请求数量。该工具特别适用于在部署前评估 Redis 的性能瓶颈或在调试时验证配置更改的影响[^3]。 ### `redis-check-aof`:AOF 持久化文件修复工具 `redis-check-aof` 用于检查和修复 Redis 的 AOF(Append-Only File)持久化文件。如果 Redis 因为异常关闭而导致 AOF 文件损坏,可以使用该工具进行恢复。例如: ```bash redis-check-aof appendonly.aof ``` 如果发现文件损坏,可以使用 `--fix` 选项进行修复: ```bash redis-check-aof --fix appendonly.aof ``` 该工具能够确保 AOF 文件的完整性,避免数据丢失或服务中断。 ### `redis-cli`:命令行客户端工具 `redis-cli` 是 Redis 的命令行客户端工具,用于与 Redis 服务器交互。它支持多种命令,例如设置键值对、查询数据、管理服务器配置等。例如,连接本地 Redis 服务器并设置一个键值对: ```bash redis-cli 127.0.0.1:6379> SET mykey "Hello Redis" OK 127.0.0.1:6379> GET mykey "Hello Redis" ``` 此外,`redis-cli` 还支持集群管理、哨兵模式配置等高级功能,是日常运维和调试的核心工具。 ### `redis-server`:启动 Redis 服务器 `redis-server` 是启动 Redis 服务器的主程序。默认情况下,它会读取配置文件(通常是 `redis.conf`)来启动实例。例如: ```bash redis-server /path/to/redis.conf ``` 配置文件中可以定义端口、持久化策略、日志路径等参数。如果需要启动多个 Redis 实例,可以通过不同的配置文件指定不同的端口和数据目录。 ### 工具常见问题及解决方案 1. **`redis-benchmark` 测试结果不准确** 可能由于网络延迟或服务器负载过高导致。建议在本地环境中进行测试,并确保服务器资源充足。 2. **`redis-check-aof` 无法修复文件** 如果 AOF 文件损坏严重,可能需要依赖备份文件进行恢复。建议定期备份 AOF 文件以防止数据丢失。 3. **`redis-cli` 无法连接服务器** 检查 Redis 服务器是否正在运行,以及防火墙是否允许相应端口通信。可以使用 `redis-cli ping` 命令测试连接状态。 4. **`redis-server` 启动失败** 检查配置文件中的参数是否正确,尤其是端口是否被占用、日志路径是否存在。可以通过查看日志文件定位问题。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值