redis简介(一)

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

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值