redis是一个高性能的缓存数据库,支持不限于key-value结构的数据缓存和持久化。直白的讲,可用于进程间通信或者说共享资源,比unix socket、共享内存、消息队列等更方便便捷,而且支持跨语言,凭借着简单实用性能高,被非常广泛应用。
Hiredis是Redis数据库的一个简单的C客户端库。它的接口只有简单的几个,但足够大多数场景使用。
这里直接用yum源安装了,这是最简单的方式、
yum install -y redis.x86_64 yum install -y hiredis-devel.x86_64
修改下/etc/redis.conf 把 daemonize no 改为yes,这样就可以在后台运行了
redis-server /etc/redis.conf #启动
首先得熟悉下redis的基本命令,更多资源看文尾的链接。下面是简单的get,set连接
root@localhost ~]# redis-cli 127.0.0.1:6379> set mykey "this is a value" OK 127.0.0.1:6379> get mykey "this is a value" 127.0.0.1:6379> quit [root@localhost ~]#
代码如下:
#include <stdio.h> #include <hiredis/hiredis.h> int main() { redisContext *conn = redisConnect("127.0.0.1", 6379); if (conn != NULL && conn->err) { printf("Connection error: %s\n", conn->errstr); return 0; } redisReply *reply = NULL; reply = redisCommand(conn, "SET %s %s", "mykey", "this is a value"); freeReplyObject(reply);reply = NULL; reply = redisCommand(conn, "GET %s", "mykey"); printf("%s\n", reply->str); freeReplyObject(reply);reply = NULL; redisFree(conn); return 0; }
编译运行:
[root@localhost ~]# gcc demo.c -lhiredis -o demo [root@localhost ~]# ./demo this is a value
当然GET和SET也可以写到不同的程序中去,结果是一样的。
在实际开发中,value可能不仅仅简单的字符串而已,而是些复合类型比如结构体或者序列化之后的数据。
代码如下
#include <stdio.h> #include <hiredis/hiredis.h> struct Block{ int id; int data; }; int main() { // 连接redis redisContext *conn = redisConnect("127.0.0.1", 6379); if (conn != NULL && conn->err) { printf("connection error: %s\n", conn->errstr); return 0; } char *key = "Block1"; // 向redis里面写入二进制数据 struct Block b; b.id = 1111; b.data = 2222; redisReply *reply; reply = redisCommand(conn, "SET %s %b", key, &b, sizeof(b)); freeReplyObject(reply);reply = NULL; // 从redis中读取数据 reply = redisCommand(conn, "GET %s", key); struct Block* bb = (struct Block*)(reply->str); if(bb == NULL) return 0; printf("id=%d\n", bb->id); printf("data=%d\n", bb->data); freeReplyObject(reply); // 删除 reply = redisCommand(conn, "DEL %s", key); freeReplyObject(reply);reply = NULL; // 释放连接 redisFree(conn); return 0; }
编译运行
[root@localhost ~]# gcc demo1.c -lhiredis -o demo1 [root@localhost ~]# ./demo1 id=1111 data=2222
经过上面的实践之后,对redis应该有一个大概的了解了 。下面再熟悉下redis的五种数据类型的常用操作
127.0.0.1:6379> set key "123456789" OK 127.0.0.1:6379> get key "123456789" 127.0.0.1:6379> getrange key 1 4 "2345" 127.0.0.1:6379> getset key "newstring" "123456789" 127.0.0.1:6379> get key "newstring" 127.0.0.1:6379> strlen key (integer) 9 127.0.0.1:6379> append key " tail" (integer) 14 127.0.0.1:6379> get key "newstring tail"
127.0.0.1:6379> lpush mykey 1stnode (integer) 1 127.0.0.1:6379> lpush mykey 2ndnode (integer) 2 127.0.0.1:6379> lpush mykey 3thnode (integer) 3 127.0.0.1:6379> LRANGE mykey 0 4 1) "3thnode" 2) "2ndnode" 3) "1stnode"
set基于hashtable实现,增删查复杂度为O(1)
127.0.0.1:6379> sadd skey hello (integer) 1 127.0.0.1:6379> sadd skey world (integer) 1 127.0.0.1:6379> sadd skey sea (integer) 1 127.0.0.1:6379> sadd skey land (integer) 1 127.0.0.1:6379> SMEMBERS skey 1) "land" 2) "world" 3) "sea" 4) "hello"
HMSET key field1 value1 [field2 value2 ]
同时将多个 field-value (域-值)对设置到哈希表 key 中。
127.0.0.1:6379> hmset hkey xiaoming "sanhaoxuesheng" xiaoqiang "tiyuweiyuan" xiaozhang "lishikedaibiao" OK 127.0.0.1:6379> hgetall hkey 1) "xiaoming" 2) "sanhaoxuesheng" 3) "xiaoqiang" 4) "tiyuweiyuan" 5) "xiaozhang" 6) "lishikedaibiao" 127.0.0.1:6379> hget hkey xiaoming "sanhaoxuesheng" 127.0.0.1:6379> hget hkey xiaoqiang "tiyuweiyuan"
127.0.0.1:6379> zadd zsetkey 98.0 xiaoming (integer) 1 127.0.0.1:6379> zadd zsetkey 97.0 xiaohong (integer) 1 127.0.0.1:6379> zadd zsetkey 92.0 xiaoqiang (integer) 1 127.0.0.1:6379> zadd zsetkey 92.0 xiaolong (integer) 1 127.0.0.1:6379> zadd zsetkey 93.0 xiaoguang (integer) 1 127.0.0.1:6379> zrange zsetkey 0 6 withscores 1) "xiaolong" 2) "92" 3) "xiaoqiang" 4) "92" 5) "xiaoguang" 6) "93" 7) "xiaohong" 8) "97" 9) "xiaoming" 10) "98"
Redis 官网:https://redis.io/
源码地址:https://github.com/redis/redis
Redis 在线测试:http://try.redis.io/
Redis 命令参考:http://doc.redisfans.com/
入门推荐 https://www.runoob.com/redis/redis-tutorial.html