Redis-C客户端-HiRedis-(一)


Redis安装步骤:

1.redis server安装

wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz #下载文件
tar xzf redis-2.4.6.tar.gz
cd redis-2.4.6
make
cp src/redis-server src/redis-cli /usr/bin/ #方便在终端在任何地方直接运行
cp redis.conf /etc/
ufw allow 6379 #ubuntu下开启端口


修改/etc/redis.conf,让server以守护进程在后台执行。
daemonize yes


2.启动redis服务
redis-server /etc/redis.conf


3.检测redis服务是否正常启动

ps -ef | grep redis


Hiredis客户端下载地址:https://github.com/redis/hiredis


Hiredis安装步骤:
tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip
cd antirez-hiredis-3cc6a7f
make


mkdir /usr/lib/hiredis
cp libhiredis.so /usr/lib/hiredis #将动态连接库libhiredis.so至/usr/lib/hiredis
mkdir /usr/include/hiredis
cp hiredis.h /usr/include/hiredis




1.连接hiredis服务器
#include <stdio.h>
#include <hiredis/hiredis.h>


redisContext *conn = redisConnect("127.0.0.1", 6379); //redis server默认端口
if(conn->err){
printf("connection error: %s", conn->str);
}


2.发送命令至服务器
redisReply *reply = redisCommand(conn, "set key value");


3.关闭Reply对象
freeReplyObject(reply);


4.关闭连接
redisFree(conn);


*************************************************

在ubuntu环境下redis开发

    1、下载redis 

      wgethttp://redis.googlecode.com/files/redis-2.4.17.tar.gz
      tar -zvxf redis-2.4.17.tar.gz
      cd redis-2.4.17
      make
      sudo make install

    2、安装hiredis
      wget https://github.com/antirez/hiredis/tarball/master
      tar xzvf master
      cd antirez-hiredis-0fff0f1
      make
      sudo make install
   3、测试验证
      开启redis-server; redis-server 
      测试代码如下:
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <hiredis/hiredis.h>  
  3.   
  4. int main()  
  5. {  
  6.     redisContext* conn = redisConnect("127.0.0.1",6379);  
  7.     if(conn->err)   printf("connection error:%s\n"
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Redis是一个开源的高性能键值存储系统,C语言是一种非常适合与Redis进行交互的语言。为了使用Redis的C客户端,你需要先安装Redis服务器,并确保你的C开发环境已经设置好。 在C语言中,你可以使用hiredis库来与Redis进行交互。hiredis是一个简单、轻量级且易于使用的Redis C客户端库,它提供了一组简洁的API来执行Redis命令和处理返回的数据。 你可以从hiredis的官方GitHub仓库(https://github.com/redis/hiredis)中获取最新版本的源代码和文档。为了使用hiredis,你需要将其源代码编译为静态库或动态库,并链接到你的C项目中。 以下是一个简单的示例,展示了如何使用hiredis库连接到Redis服务器并执行一些基本的操作: ```c #include <stdio.h> #include <hiredis.h> int main() { redisContext *ctx = redisConnect("localhost", 6379); // 连接到本地Redis服务器 if (ctx == NULL || ctx->err) { if (ctx) { printf("Error: %s\n", ctx->errstr); redisFree(ctx); } else { printf("Can't allocate redis context\n"); } return 1; } redisReply *reply = redisCommand(ctx, "SET mykey hello"); // 执行SET命令 if (reply == NULL) { printf("Failed to execute command\n"); } else { printf("SET: %s\n", reply->str); freeReplyObject(reply); } reply = redisCommand(ctx, "GET mykey"); // 执行GET命令 if (reply == NULL) { printf("Failed to execute command\n"); } else { printf("GET: %s\n", reply->str); freeReplyObject(reply); } redisFree(ctx); // 断开与Redis服务器的连接 return 0; } ``` 这只是一个简单的示例,实际上hiredis库提供了更多的API和功能,如事务、管道、订阅/发布等。你可以查阅hiredis的官方文档以了解更多详细信息和示例代码。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值