Redis的五大数据类型

官网可查看命令: http://www.redis.cn/commands.html

Redis-key

~~~shell 127.0.0.1:6379> set name xxx OK 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> set age 1 OK 127.0.0.1:6379> keys * 1) "age" 2) "name"

127.0.0.1:6379> exists name # 判断key 是否存在 (integer) 1 127.0.0.1:6379> exists name1 (integer) 0 127.0.0.1:6379> move name 1 # 移到1号库 (integer) 1

127.0.0.1:6379> keys * 1) "age" 127.0.0.1:6379> set name yyy OK 127.0.0.1:6379> expire name 10 # 设置key的过期时间,单位是秒 (integer) 1 127.0.0.1:6379> ttl name # 查看当前key的剩余过期时间 (integer) 7 127.0.0.1:6379> ttl name (integer) -2 127.0.0.1:6379> type age # 查看当前key的类型 string 127.0.0.1:6379> ~~~

Redis 有以下 5 种基本的数据类型

String

  • set key value(String 数据类型)

~~~shell 127.0.0.1:6379> set key1 v1 #设置值 OK 127.0.0.1:6379> get key1 "v1" 127.0.0.1:6379> append key1 "hello" # 追加值,如果不存在,相当于 set key (integer) 7 127.0.0.1:6379> get key1 "v1hello" 127.0.0.1:6379> strlen key1 # 获取字符串长度 (integer) 7 127.0.0.1:6379> ~~~

  • 自增、自减

~~~shell 127.0.0.1:6379> set views 0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views # 自增 1 (integer) 1 127.0.0.1:6379> get views "1" 127.0.0.1:6379> decr views # 自减 1 (integer) 0 127.0.0.1:6379> decr views (integer) -1 127.0.0.1:6379> get views "-1" 127.0.0.1:6379> incrby views 10 # 设置步长、自增 10 (integer) 9 127.0.0.1:6379> decrby views 5 # 设置步长、自减 5 (integer) 4 ~~~

  • 字符串范围:

~~~shell 127.0.0.1:6379> set key1 "hello,world!" OK 127.0.0.1:6379> get key1 "hello,world!" 127.0.0.1:6379> getrange key1 0 3 # 截取字符串[0, 3] "hell" 127.0.0.1:6379> getrange key1 0 -1 # 获取全部的字符串,和 get key一样 -1代表尾部 "hello,world!" 127.0.0.1:6379> ~~~

  • 替换:

~~~shell 127.0.0.1:6379> set key2 abcdefg OK 127.0.0.1:6379> get key2 "abcdefg" 127.0.0.1:6379> setrange key2 1 xx (integer) 7 127.0.0.1:6379> get key2 "axxdefg" 127.0.0.1:6379> ~~~

  • setex(set with expire):设置过期时间

  • setnx(set if not exist):不存在再设置(在分布式锁中会经常使用)

~~~shell 127.0.0.1:6379> setex key3 30 "hello" # 设置 30 秒后过期 OK 127.0.0.1:6379> ttl key3 # 剩余过期时间 (integer) 25 127.0.0.1:6379> setnx mykey "redis" # mykey 不存在时设置成功 (integer) 1 127.0.0.1:6379> keys * 1) "key2" 2) "key1" 3) "views" 4) "mykey" 127.0.0.1:6379> setnx mykey "mongoDB" # mykey 存在时设置失败 (integer) 0 127.0.0.1:6379> get mykey # mykey 值不变 "redis" 127.0.0.1:6379> ~~~

  • msetmget

~~~shell 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # 同时设置多个值 OK 127.0.0.1:6379> keys * 1) "k1" 2) "k3" 3) "k2" 127.0.0.1:6379> mget k1 k2 k3 # 同时获取多个值 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> msetnx k1 v1 k4 v4 # msetnx 是一个原子性的操作,要么一起成功,要么都失败 (integer) 0 127.0.0.1:6379> get k4 (nil) 127.0.0.1:6379> ~~~

  • 对象

~~~shell set user:1 {name:zhangsan, age:3} # 设置一个 user:1 对象 值为 json 字符来保存一个对象

127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2 OK 127.0.0.1:6379> mget user:1:name user:1:age 1) "zhangsan" 2) "2" 127.0.0.1:6379> ~~~

  • getset:先 get 再 set

~~~shell 127.0.0.1:6379> getset db redis # 如果不存在值,则返回 nil (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db mongodb # 如果存在值,获取原来的值,并设置新的值 "redis" 127.0.0.1:6379> get db "mongodb" 127.0.0.1:6379> ~~~

List

基本的数据类型,列表。

在 Redis 中可以把 list 用作栈、队列、阻塞队列。

list 命令以 l开头 一般代表 队列左侧执行,r代表队列右侧执行。

~~~shell 127.0.0.1:6379> lpush list one # 将一个值或者多个值,插入到列表的头部(左) (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379> lrange list 0 -1 # 查看全部元素 -1代表尾部 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> lrange list 0 1 # 通过区间获取值 1) "three" 2) "two" 127.0.0.1:6379> rpush list right # 将一个值或者多个值,插入到列表的尾部(右) (integer) 4 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 4) "right" 127.0.0.1:6379> ~~~

  • 弹出 pop

~~~shell 127.0.0.1:6379> lrange list 0 -1 1) "!" 2) "world" 3) "world" 4) "hello" 127.0.0.1:6379> lpop list # 移除list的第一个元素 "!" 127.0.0.1:6379> lrange list 0 -1 1) "world" 2) "world" 3) "hello" 127.0.0.1:6379> rpop list # 移除list的第一个元素 "hello" 127.0.0.1:6379> lrange list 0 -1 1) "world" 2) "world" 127.0.0.1:6379> ~~~

  • 索引 lindex

~~~shell 127.0.0.1:6379> lrange list 0 -1 1) "hjk" 2) "world" 3) "world" 127.0.0.1:6379> lindex list 1 # 通过下标获取list中的某一个值 "world" 127.0.0.1:6379> lindex list 0 "hjk" 127.0.0.1:6379> ~~~

  • llen 长度:

~~~shell 127.0.0.1:6379> llen list (integer) 3 127.0.0.1:6379> ~~~

  • 移除指定的值:

~~~shell 127.0.0.1:6379> lrange list 0 -1 1) "hjk" 2) "world" 3) "world" 127.0.0.1:6379> lrem list 1 world # 移除list集合中指定个数的value,精确匹配 (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "hjk" 2) "world" 127.0.0.1:6379> lpush list hjk (integer) 3 127.0.0.1:6379> lrange list 0 -1 1) "hjk" 2) "hjk" 3) "world" 127.0.0.1:6379> lrem list 2 hjk (integer) 2 127.0.0.1:6379> lrange list 0 -1 1) "world" 127.0.0.1:6379> ~~~

  • ltrim 截断

~~~shell 127.0.0.1:6379> lrange mylist 0 -1 1) "hello1" 2) "hello2" 3) "hello3" 4) "hello4" 127.0.0.1:6379> ltrim mylist 1 2 # 通过下标截取指定长度,这个list已经被破坏了,截断之后只剩下截断后的元素 OK 127.0.0.1:6379> lrange mylist 0 -1 1) "hello2" 2) "hello3" 127.0.0.1:6379> ~~~

  • rpoplpush :移除列表的最后一个元素,将他移动到新的列表中。

~~~shell 127.0.0.1:6379> lrange mylist 0 -1 1) "hello1" 2) "hello2" 3) "hello3" 127.0.0.1:6379> rpoplpush mylist myotherlis # 移除列表的最后一个元素,将他移动到新的列表中 "hello3" 127.0.0.1:6379> lrange mylist 0 -1 # 查看原来的列表 1) "hello1" 2) "hello2" 127.0.0.1:6379> lrange myotherlist 0 -1 # 查看目标列表中,确实存在该值 1) "hello3" 127.0.0.1:6379> ~~~

  • lset:将列表中指定下标的值替换为另一个值,更新操作

~~~shell 127.0.0.1:6379> exists list # 判断这个列表是否存在 (integer) 0 127.0.0.1:6379> lset list 0 item # 如果不存在的话,更新会报错 (error) ERR no such key 127.0.0.1:6379> lpush list value1 (integer) 1 127.0.0.1:6379> lrange list 0 0 1) "value1" 127.0.0.1:6379> lset list 0 item # 如果存在,更新当前下标的值 OK 127.0.0.1:6379> lset list 1 other # 如果不存在的话,更新会报错 (error) ERR index out of range 127.0.0.1:6379> ~~~

  • linsert:将某个具体的value插入到列表中某个元素的前面或者后面

~~~shell 127.0.0.1:6379> lrange mylist 0 -1 1) "hello1" 2) "hello2" 127.0.0.1:6379> linsert mylist before "hello2" hello (integer) 3 127.0.0.1:6379> lrange mylist 0 -1 1) "hello1" 2) "hello" 3) "hello2" 127.0.0.1:6379> linsert mylist after "hello2" hello (integer) 4 127.0.0.1:6379> lrange mylist 0 -1 1) "hello1" 2) "hello" 3) "hello2" 4) "hello" 127.0.0.1:6379> ~~~

小结

  • list 实际上是一个链表,前后都可以插入
  • 如果key不存在,创建新的链表
  • 如果移除了所有的值,空链表,也代表不存在
  • 在两边插入或者改动值,效率最高。

Set

~~~shell 127.0.0.1:6379> sadd myset "hello" # set 集合中添加元素 (integer) 1 127.0.0.1:6379> sadd myset "world"
(integer) 1 127.0.0.1:6379> smembers myset # 查看指定Set的所有值 1) "world" 2) "hello" 127.0.0.1:6379> sismember myset hello # 判断某一个值是不是在set中 (integer) 1 127.0.0.1:6379> sismember myset hello1 (integer) 0 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> scard myset # 获取集合中的个数 (integer) 2 127.0.0.1:6379> sadd myset "hello2"
(integer) 1 127.0.0.1:6379> smembers myset
1) "world" 2) "hello2" 3) "hello" 127.0.0.1:6379> srem myset hello # 移除元素 (integer) 1 127.0.0.1:6379> smembers myset 1) "world" 2) "hello2" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> smembers myset 1) "kkk" 2) "world" 3) "hjk" 4) "hello2" 127.0.0.1:6379> srandmember myset # 随机抽取一个元素 "hjk" 127.0.0.1:6379> srandmember myset "hello2" 127.0.0.1:6379> srandmember myset 2 # 随机抽取指定个数的元素 1) "world" 2) "hello2" 127.0.0.1:6379> srandmember myset 2 1) "hello2" 2) "hjk" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> smembers myset 1) "kkk" 2) "world" 3) "hjk" 4) "hello2" 127.0.0.1:6379> spop myset # 随机删除元素 "hjk" 127.0.0.1:6379> smembers myset 1) "kkk" 2) "world" 3) "hello2" 127.0.0.1:6379> spop myset "hello2" 127.0.0.1:6379> smembers myset 1) "kkk" 2) "world" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> smembers myset 1) "kkk" 2) "world" 127.0.0.1:6379> sadd myset2 set2 (integer) 1 127.0.0.1:6379> smove myset myset2 "kkk" # 将一个特定的值,移动到另一个set集合中 (integer) 1 127.0.0.1:6379> smembers myset 1) "world" 127.0.0.1:6379> smembers myset2 1) "kkk" 2) "set2" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> smembers key1 1) "b" 2) "a" 3) "c" 127.0.0.1:6379> smembers key2 1) "e" 2) "d" 3) "c" 127.0.0.1:6379> sdiff key1 key2 # 差集 1) "b" 2) "a" 127.0.0.1:6379> sinter key1 key2 # 交集 1) "c" 127.0.0.1:6379> sunion key1 key2 # 并集 1) "e" 2) "a" 3) "c" 4) "d" 5) "b" ~~~

Hash

  • key field value

~~~shell 127.0.0.1:6379> hset myhash field xxx # set 一个 key-value (integer) 1 127.0.0.1:6379> hget myhash field # 获取一个字段值 "xxx" 127.0.0.1:6379> hmset myhash field1 hello field2 world # set 多个 key-value OK 127.0.0.1:6379> hmget myhash field field1 field2 # 获取多个字段值 1) "xxx" 2) "hello" 3) "world" 127.0.0.1:6379> hgetall myhash # 获取全部的数据 1) "field" 2) "xxx" 3) "field1" 4) "hello" 5) "field2" 6) "world" ~~~

~~~shell 127.0.0.1:6379> hdel myhash field1 # 删除指定的key,对应的value也就没有了 (integer) 1 127.0.0.1:6379> hgetall myhash 1) "field" 2) "xxx" 3) "field2" 4) "world" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> hlen myhash # 获取长度 (integer) 2 127.0.0.1:6379> hexists myhash field1 # 判断指定key是否存在 (integer) 0 127.0.0.1:6379> hexists myhash field2 (integer) 1 127.0.0.1:6379> hkeys myhash # 获取所有的key 1) "field" 2) "field2" 127.0.0.1:6379> hvals myhash # 获取所有的value 1) "xxx" 2) "world" 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> hset myhash field3 5
(integer) 1 127.0.0.1:6379> hincrby myhash field3 1 # 指定增量 (integer) 6 127.0.0.1:6379> hincrby myhash field3 -1 (integer) 5 127.0.0.1:6379> hsetnx myhash field4 hello # 如果不存在则可以设置 (integer) 1 127.0.0.1:6379> hsetnx myhash field4 world # 如果存在则不能设置 (integer) 0 127.0.0.1:6379> ~~~

2.1.5 ZSet

  • zadd key score value

~~~shell 127.0.0.1:6379> zadd myset 1 one # 添加一个值 (integer) 1 127.0.0.1:6379> zadd myset 2 two 3 three # 添加多个值 (integer) 2 127.0.0.1:6379> zrange myset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> ~~~

  • 实现排序:

~~~shell 127.0.0.1:6379> zadd salary 2500 xiaohong (integer) 1 127.0.0.1:6379> zadd salary 5000 xiaoming (integer) 1 127.0.0.1:6379> zadd salary 500 xaiozhang (integer) 1

127.0.0.1:6379> zrange salary 0 -1 # 从小到大排序 1) "xaiozhang" 2) "xiaohong" 3) "xiaoming"

127.0.0.1:6379> zrevrange salary 0 -1 # 从大到小进行排序 1) "xiaoming" 2) "xiaohong" 3) "xaiozhang"

127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores# 显示工资小于 2500 的用户 1) "xaiozhang" 2) "500" 3) "xiaohong" 4) "2500" 127.0.0.1:6379> zrangebyscore salary 1000 +inf withscores # 显示工资大于1000 的用户 ~~~

~~~shell 127.0.0.1:6379> zrange salary 0 -1 1) "xaiozhang" 2) "xiaohong" 3) "xiaoming" 127.0.0.1:6379> zrem salary xiaohong # 移除特定元素 (integer) 1 127.0.0.1:6379> zrange salary 0 -1 1) "xaiozhang" 2) "xiaoming" 127.0.0.1:6379> zcard salary # 获取有序集合的个数 (integer) 2 127.0.0.1:6379> ~~~

~~~shell 127.0.0.1:6379> zadd myset 1 hello (integer) 1 127.0.0.1:6379> zadd myset 2 world 3 ! (integer) 2 127.0.0.1:6379> zcount myset 1 3 # 获取指定区间的人员数量 (integer) 3 127.0.0.1:6379> zcount myset 1 2 (integer) 2 ~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值