标签:127.0 0.1 简介 redis 6379 key reply
0. redis是什么
redis是一个高性能的缓存数据库,支持不限于key-value结构的数据缓存和持久化。直白的讲,可用于进程间通信或者说共享资源,比unix socket、共享内存、消息队列等更方便便捷,而且支持跨语言,凭借着简单实用性能高,被非常广泛应用。
Hiredis是Redis数据库的一个简单的C客户端库。它的接口只有简单的几个,但足够大多数场景使用。
1. 安装
1.1 运行环境和 C语言客户端库
这里直接用yum源安装了,这是最简单的方式、
yum install -y redis.x86_64
yum install -y hiredis-devel.x86_64
1.2 后台运行
修改下/etc/redis.conf 把 daemonize no 改为yes,这样就可以在后台运行了
redis-server /etc/redis.conf #启动
2. hiredis
首先得熟悉下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 ~]#
2.1 一个简单的例子
代码如下:
#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也可以写到不同的程序中去,结果是一样的。
2.2 二进制数据作为value
在实际开发中,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
3. 基本的数据结构
经过上面的实践之后,对redis应该有一个大概的了解了 。下面再熟悉下redis的五种数据类型的常用操作
3.1 string 字符串
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"
3.2 list 列表
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"
3.3 set 集合
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"
3.4 hash
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"
3.5 有序集合
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
标签:127.0,0.1,简介,redis,6379,key,reply 来源: https://blog.csdn.net/niu91/article/details/116545732
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。