Redis五大数据类型
String
append key "hello" #追加字符串,如果这个key不存在,等价set
strlen key #查看字符串长度
set key2 0 #0
incr key2 #1
decr key2 #0
incrby key2 10 #10
decrby key2 5 #5
127.0.0.1:6379> set key3 hello,hja
OK
127.0.0.1:6379> get key3
"hello,hja"
127.0.0.1:6379> getrange key3 0 5 #截取字符串,从1到6
"hello,"
127.0.0.1:6379> getrange key3 0 -1 #全部
"hello,hja"
127.0.0.1:6379> set key4 abcdefg
OK
127.0.0.1:6379> setrange key4 2 xxx #从3开始做替换
(integer) 7
127.0.0.1:6379> get key4
"abxxxfg"
127.0.0.1:6379> setex key5 30 hello #(set with expire) 设置过期时间 30秒
OK
127.0.0.1:6379> get key5
"hello"
127.0.0.1:6379> ttl key5
(integer) 9
127.0.0.1:6379> setnx key6 hja #(set if not exist)不存在再设置,在分布式锁中常常使用
(integer) 1
127.0.0.1:6379> setnx key6 hjaaa #key6存在,设置失败
(integer) 0
127.0.0.1:6379> get key6
"hja"
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 #同时设置多个值
OK
127.0.0.1:6379> mget k1 k2 k3 #同时获取多个值
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v111 k4 v4 #msetnx是一个原子性的操作,要么一起成功,要么一起失败
(integer) 0
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> set user:1 {name:hja,age:21} #设置一个user:1对象 值为json字符来保存一个对象
OK
127.0.0.1:6379> keys *
1) "user:1"
127.0.0.1:6379> get user:1
"{name:hja,age:21}"
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> keys *
1) "user:1:name"
2) "user:1:age"
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> getset key v1 #先get再set,如果key不存在就只set
(nil)
127.0.0.1:6379> keys *
1) "key"
127.0.0.1:6379> get key
"v1"
127.0.0.1:6379> getset key v2
"v1"
127.0.0.1:6379> get key
"v2"
List(列表)
可以把list玩成栈,队列,阻塞队列
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) "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 zero #存值,插入到列表尾部(右)
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "zero"
127.0.0.1:6379> lpop list
"three" #移除第一个值
127.0.0.1:6379> rpop list
"zero" #移除最后一个值
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list 0 #按index取值
"two"
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> llen list
(integer) 2 #查看列表长度
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lrem list 2 three #移除两个three
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lrange list 0 -1
1) "hello4"
2) "hello3"
3) "hello2"
4) "hello1"
127.0.0.1:6379> ltrim list 0 1 #通过下标截取指定长度,list被改变,只剩下截取的元素
OK
127.0.0.1:6379> lrange list 0 -1
1) "hello4"
2) "hello3"
127.0.0.1:6379> lrange list 0 -1
1) "hello3"
2) "hello2"
3) "hello1"
127.0.0.1:6379> lrange list2 0 -1
1) "hello2"
127.0.0.1:6379> rpoplpush list list2 #移除列表的最后一个元素,将他移动到其他列表中
"hello1"
127.0.0.1:6379> lrange list 0 -1
1) "hello3"
2) "hello2"
127.0.0.1:6379> lrange list2 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> lrange list 0 -1
1) "value2"
2) "value1"
127.0.0.1:6379> lset list 0 hja #通过下标更新值
OK
127.0.0.1:6379> lrange list 0 -1
1) "hja"
2) "value1"
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> linsert list before world v1 #将某个具体的值插入到列中某个元素的前面或后面
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "v1"
3) "world"
127.0.0.1:6379> linsert list after world v2
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "v1"
3) "world"
4) "v2"
小结
list实际上是一个链表,before Node after , left , right 都可以插入值
如果key不存在,创建新的链表
如果key存在,新增内容
如果移除了所有值,空链表,也代表不存在!
在两边插入或者改动值,效率最高!中间元素,相对来说效率会低一点
127.0.0.1:6379> lpush list hello
(integer) 1
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379> lrem list 1 hello
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
Set(无序集合)
set中的值无序且不能重复
127.0.0.1:6379> sadd myset hello #添加
(integer) 1
127.0.0.1:6379> sadd myset hello
(integer) 0
127.0.0.1:6379> sadd myset hja
(integer) 1
127.0.0.1:6379> smembers myset #查看指定set的所有值
1) "hja"
2) "hello"
127.0.0.1:6379> sismember myset hello #查看指定set是否含有某个值
(integer) 1
127.0.0.1:6379> sismember myset world
(integer) 0
127.0.0.1:6379> sismember myset "hello"
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2 #获取set中值的个数
127.0.0.1:6379> srem myset hello #移除元素
(integer) 1
127.0.0.1:6379> smembers myset
1) "hja"
127.0.0.1:6379> keys *
1) "myset"
127.0.0.1:6379> srem myset hja
(integer) 1
127.0.0.1:6379> keys *
(empty list or set) #set集合元素删光后set也就没了
127.0.0.1:6379> smembers myset
1) "v3"
2) "v1"
3) "v4"
4) "v2"
127.0.0.1:6379> srandmember myset #获取随机一个数
"v1"
127.0.0.1:6379> srandmember myset
"v3"
127.0.0.1:6379> srandmember myset
"v4"
127.0.0.1:6379> srandmember myset
"v4"
127.0.0.1:6379> srandmember myset
"v2"
127.0.0.1:6379> srandmember myset
"v3"
127.0.0.1:6379> srandmember myset 2 #获取随机两个数
1) "v3"
2) "v4"
127.0.0.1:6379> srandmember myset 2
1) "v3"
2) "v2"
127.0.0.1:6379> srandmember myset 2
1) "v1"
2) "v4"
127.0.0.1:6379> smembers myset
1) "v3"
2) "v1"
3) "v4"
4) "v2"
127.0.0.1:6379> spop myset #随机移除一个元素
"v3"
127.0.0.1:6379> spop myset
"v1"
127.0.0.1:6379> smembers set1
1) "v3"
2) "v1"
3) "v2"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v5"
3) "v4"
127.0.0.1:6379> smove set1 set2 v1 #把set1中的v1移到set2中
(integer) 1
127.0.0.1:6379> smembers set1
1) "v3"
2) "v2"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v1"
3) "v5"
4) "v4"
127.0.0.1:6379> smove set1 set2 v3 #set1和set2都有v3,所以移动v3时set1的v3消失,set2不变
(integer) 1
127.0.0.1:6379> smembers set1
1) "v2"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v1"
3) "v5"
4) "v4"
127.0.0.1:6379> smembers set1
1) "v3"
2) "v1"
3) "v2"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v5"
3) "v4"
127.0.0.1:6379> sdiff set1 set2 #差集,set1有而set2没有的元素
1) "v1"
2) "v2"
127.0.0.1:6379> sinter set1 set2 #交集,set1和set2都有的元素
1) "v3"
127.0.0.1:6379> sunion set1 set2 #并集,set1和set2中的所有元素组成的集合
1) "v3"
2) "v1"
3) "v5"
4) "v2"
5) "v4"
#多个集合也同样适用
127.0.0.1:6379> sadd set3 v3
(integer) 1
127.0.0.1:6379> sadd set3 v4
(integer) 1
127.0.0.1:6379> sadd set3 v6
(integer) 1
127.0.0.1:6379> sdiff set1 set2 set3
1) "v1"
2) "v2"
127.0.0.1:6379> sinter set1 set2 set3
1) "v3"
127.0.0.1:6379> sunion set1 set2 set3
1) "v6"
2) "v2"
3) "v5"
4) "v4"
5) "v3"
6) "v1"
Hash
127.0.0.1:6379> hset myhash field1 v1 #设置一个k-v对
(integer) 1
127.0.0.1:6379> hget myhash field1 #根据k获取v
"v1"
127.0.0.1:6379> hmset myhash field2 v2 field3 v3 #设置多个k-v对
OK
127.0.0.1:6379> hmget myhash field1 field2 field3 #根据多个k获取v
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> hgetall myhash #获取全部数据
1) "field1"
2) "v1"
3) "field2"
4) "v2"
5) "field3"
6) "v3"
127.0.0.1:6379> hdel myhash field2 field1 #删除指定字段及其数据
(integer) 2
127.0.0.1:6379> hgetall myhash
1) "field3"
2) "v3"
127.0.0.1:6379> hmset myhash field1 v1 field2 v2 field3 v3
OK
127.0.0.1:6379> hgetall myhash
1) "field3"
2) "v3"
3) "field1"
4) "v1"
5) "field2"
6) "v2"
127.0.0.1:6379> hlen myhash
(integer) 3 #获取hash表的字段数
127.0.0.1:6379> hexists myhash field1 #判断指定字段是否存在
(integer) 1
127.0.0.1:6379> hkeys myhash #获取所有字段
1) "field3"
2) "field1"
3) "field2"
127.0.0.1:6379> hvals myhash #获取所有value
1) "v3"
2) "v1"
3) "v2"
127.0.0.1:6379> hset myhash field4 3
(integer) 1
127.0.0.1:6379> hincrby myhash field4 2 #字段的值加2
(integer) 5
127.0.0.1:6379> hincrby myhash field4 2
(integer) 7
127.0.0.1:6379> hincrby myhash field4 -1 #字段值减一
(integer) 6
127.0.0.1:6379> hgetall myhash
1) "field3"
2) "v3"
3) "field1"
4) "v1"
5) "field2"
6) "v2"
7) "field4"
8) "6"
127.0.0.1:6379> hsetnx myhash field4 1 #如果不存在字段field4设置field4字段的值为1,存在的话设置失败
(integer) 0
127.0.0.1:6379> hsetnx myhash field5 1
(integer) 1
Hash适合存储对象,String适合存储字符串,例:
127.0.0.1:6379> hmset user:1 name hja age 21
OK
127.0.0.1:6379> hgetall user:1
1) "name"
2) "hja"
3) "age"
4) "21"
Zset(有序集合)
127.0.0.1:6379> zadd myzset 1 hello 2 world 3 hja #存值
(integer) 3
127.0.0.1:6379> zrange myzset 0 -1 #遍历取值
1) "hello"
2) "world"
3) "hja"
127.0.0.1:6379> zadd salary 1000 zhangsan 5000 lisi 3000 wangwu #输入三个用户的薪水
(integer) 3
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf #通过score(这里的数字)的大小从小到大排序 -inf是负无穷 +inf正无穷
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores #携带score一起遍历
1) "zhangsan"
2) "1000"
3) "wangwu"
4) "3000"
5) "lisi"
6) "5000"
127.0.0.1:6379> zrangebyscore salary -inf 3000 withscores #score最大值设置为3000
1) "zhangsan"
2) "1000"
3) "wangwu"
4) "3000"
127.0.0.1:6379> zrevrange salary 0 -1 #降序
1) "lisi"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "lisi"
2) "5000"
3) "wangwu"
4) "3000"
5) "zhangsan"
6) "1000"
127.0.0.1:6379> zrevrangebyscore salary +inf -inf withscores #通过score的大小从大到小排序,并且携带score
1) "lisi"
2) "5000"
3) "wangwu"
4) "3000"
5) "zhangsan"
6) "1000"
127.0.0.1:6379> zrem salary zhangsan #移除指定元素
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "wangwu"
2) "lisi"
127.0.0.1:6379> zcard salary
(integer) 2 #获取zset中值的个数
127.0.0.1:6379> zadd myzset 1 v1 3 v3 5 v5 6 v6
(integer) 4
127.0.0.1:6379> zrange myzset 0 -1
1) "v1"
2) "v3"
3) "v5"
4) "v6"
127.0.0.1:6379> zcount myzset 1 5 #获取指定区间的成员数量
(integer) 3
127.0.0.1:6379> zcount myzset 1 4
(integer) 2
127.0.0.1:6379> zcount myzset 1 3
(integer) 2