Five data types of Redis

Five data types of Redis

Redis is an open source (BSD licensed), in-memory data structure storage system, which can be used as a database, cache and consumer

Information middleware MQ. It supports multiple types of data structures, such as strings, hashes, lists

(lists), sets, sorted sets and range queries, bitmaps, hyperloglogs and geospatial

(geospatial) Index radius query. Redis has built-in replication (replication), LU scripting (Luascripting), LRU

Drive events (LRU eviction), transactions (transactions) and different levels of disk persistence (persistence), and

High availability is provided through Redis Sentinel and automatic partitioning (Cluster).

Basic commands of RedisKey> EXISTS name//判断name是否存在
Insert picture description here> move age 1//将当前key移除到另外一个数据库中
Insert picture description here> EXPIRE name 10//EXPIRE 过期 设置key的过期时间,单位是秒>ttl key //查看当前key的剩余时间
Insert picture description here> type age//查看当前key是什么类型
Insert picture description here

String string details> set key1 v1//设置值
OK> APPEND key1 "牛马"//追加值 如果key不存在,则新建一个新的key
(integer) 8> keys *//查看所有key
1) "age"
2) "key1"> get key1 //获得值
"v1\xe7\x89\x9b\xe9\xa9\xac"> EXISTS key1 //判断某一个key是否存在> STRLEN key1//获取字符串的长度
Insert picture description here
Step size> set views 0 //初始浏览量为0
OK> get views
"0"> incr views //自增1 
(integer) 1> incr views 
(integer) 2> get views
"2"> decr views  //自减1
(integer) 1> decr views
(integer) 0> get views
"0"> INCRBY views 100 //可以设置步长,指定增量
(integer) 100> INCRBY views 100
(integer) 200> get views
"200"> DECRBY views 100//可以设置步长,减少增量
(integer) 100> DECRBY views 100
(integer) 0> get views
Insert picture description here
range> set key1 "hello chufeng"
OK> get 可以
(nil)> get key1
"hello chufeng"> GETRANGE key1 0 3 //截取字符串
"hell"> GETRANGE key1 0 -1//截取全部字符串
replace> set key2 abcdefg//设置值
OK> SETRANGE key2 1 xx//替换指定位置开始的字符串
(integer) 7> get key2
#setex(set with expire)
#Set expiration time# setnx(set if not exist)#does not exist in the setting (used often in distributed locks)> set key3 60
OK> setex key3 30 "hello"//设置key3的值为hello  30s后过期
OK> get key3
"hello"> ttl key3
(integer) 23> keys *
1) "key2"
2) "key1"
3) "key3"> ttl key3
(integer) 4> keys *
1) "key2"
2) "key1"
3) "key3"> keys *
1) "key2"
2) "key1"> setnx mykey "redis"//如果mykey不存在则会创建
(integer) 1> setnx mykey "nv"//如果存在  则失败  不会替换
(integer) 0
mset mget operates multiple at the same time> mset k1 v1 k2 v2 k3 v3//设置多个值
OK> keys *
1) "key2"
2) "key1"
3) "k2"
4) "k3"
5) "k1"
6) "mykey"> get k1
"v1"> mget k1 k2 k3  //同时获取多个值
1) "v1"
2) "v2"
3) "v3"> msetnx k1 v1 k4 v4//是一个原子性操作,要么一起成功,要么一起失败
(integer) 0> get k4


set user:1{name:zhangsan,age:3}Set a user:1 object with a value of json characters to save an object

The key here is a clever design: user:{id}:{filed}, this design is completely ok in redis> mset user:1:name zhangsan user:1:age 2 
OK> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
getset #get in set first> getset db  "redis"//如果不存在,则返回uil
(nil)> getset db "mysql"//如果存在值,获取原来的值,在去设置新的值
"redis"> get db

The data structure is the same

String similar usage scenarios: value can be our number in addition to our string!
●Count the number of multi-units
● Number of fans
●Object cache storage! I

List type detailed explanation

Insert picture description here

In redis, we can play lists as stacks, queues, and blocking queues!
All list commands start with l, redis is not case sensitive> LPUSH list one//将一个值 或多个值插入到列表的头部(左)
(integer) 1> LPUSH list two
(integer) 2> LPUSH list three
(integer) 3> LRANGE list 0 -1//获取list中的所有值
1) "three"
2) "two"
3) "one"> LRANGE list 0 1//通过区间获取具体的值
1) "three"
2) "two"> RPUSH list right//将一个值 或多个值插入到列表的尾部(右)
(integer) 4> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
RPOP> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"> Lpop list//移除list 的第一个元素
"three"> RPOP list//移除list的最后一个元素
"right"> LRANGE list 0 -1
1) "two"
2) "one"
Lindex gets the value by subscript> Lrange list 0 -1
1) "two"
2) "one"> Lindex list 0//通过下标获取值
"two"> Lindex list 1
Llen list length> Llen list//返回列表的长度
(integer) 2
Remove the specified value> LPUSH list two
(integer) 3> lrem list 1 one//移除list集合中指定个数的value,精确匹配
(integer) 1> LRANGE list 0 -1
1) "two"
2) "two"> lrem list 2 two
(integer) 2> LRANGE list 0 -1
(empty array)
trim: list truncation> RPUSH mylist "hello gouzi"
(integer) 1> RPUSH mylist "hello zhuzi"
(integer) 2> RPUSH mylist "hello niuzi"
(integer) 3> RPUSH mylist "hello mazi"
(integer) 4> ltrim mylist 1 2//通过下标截取指定的长度,这个list已经被改变了,只剩下截取的元素
OK> lrange mylist 0 -1
1) "hello zhuzi"
2) "hello niuzi"
rpoplpush#Remove the last element of the list and add it to another list> rpush mylist 'woaini'
(integer) 1> rpush mylist 'woaini1'
(integer) 2> rpush mylist 'woaini2'
(integer) 3> rpoplpush mylist myotherlist//移除列表的最后一个元素,并添加到另外一个列表中
"woaini2"> lrange mylist 0 -1//查看原来的列表
1) "woaini"
2) "woaini1"> lrange myotherlist 0 -1//查看目标列表
1) "woaini2"
lset: replace the value of the specified subscript in the list, update operation> Exists list//判断这个列表是否存在
(integer) 0> lset list 0 item//如果不存在列表,我们更新就会报错
(error) ERR no such key> lpush list values1
(integer) 1> LRANGE list 0 0
1) "values1"> lset list 0 item//如果存在会替换当前下标的值
OK> LRANGE list 0 0
1) "item"
Linsert: Insert a specific value before or after an element> rpush mylist "hello"
(integer) 1> rpush mylist "world"
(integer) 2> LINSERT MYLIST BEFORE "world" "other"
(integer) 0> LINSERT mylist BEFORE "world" "other"
(integer) 3> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"> LINSERT mylist after "world" "new"
(integer) 4> lrange mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"


●It is actually a linked list, before Node after, left and right can insert values
●If the key does not exist, create a new linked list
●If the key exists, add content
●If all values ​​are removed, the empty linked list also represents Does not exist!
●Insert or change the value on both sides, the most efficient! The middle element, relatively speaking, the efficiency will be a little bit lower ~
message queue! Message queue (Lpush Rpop), stack (Lpush Lpop)

Set collection type detailed> sadd myset "hello"//set集合中添加一个元素
(integer) 1> sadd myset "chufeng"
(integer) 1> sadd myset "lovechufeng"
(integer) 1> SMEMBERS myset//查看set中的所有指定元素
1) "chufeng"
2) "hello"
3) "lovechufeng"> SISMEMBER myset hello//判断某一个值是否在set集合中
(integer) 1> SISMEMBER myset map
(integer) 0> scard myset//获取set集合中的元素个数
(integer) 3> srem myset hello//移除set集合中的指定元素
(integer) 1> smembers myset
1) "chufeng"
2) "lovechufeng"
set Unordered non-repeating collection. Random!> SRANDMEMBER myset//随机抽选出一个元素
"lovechufeng2"> SRANDMEMBER myset
"chufeng"> SRANDMEMBER myset
"chufeng"> SRANDMEMBER myset
"lovechufeng"> SRANDMEMBER myset
"lovechufeng"> SRANDMEMBER myset
"lovechufeng2"> SRANDMEMBER myset
"chufeng"> SRANDMEMBER myset 2//随机抽出指定个数的元素
1) "chufeng"
2) "lovechufeng2"
Delete the specified key, delete the key randomly!> auth 123456//输入redis密码
1) "lovechufeng2"
2) "chufeng"
3) "lovechufeng"> spop myset//随机移除元素
"lovechufeng2"> spop myset
"lovechufeng"> SMEMBERS myset
1) "chufeng"
Move a specified value to another set collection> sadd myset "wo"
(integer) 1> sadd myset "shi"
(integer) 1> sadd myset "xiao"
(integer) 1> sadd myset "chu"
(integer) 1> sadd myset "feng"
(integer) 1> sadd myset2 "set2"
(integer) 1> smove myset myset2 "feng"//将set集合中的指定元素移动到另一个集合中
(integer) 1> SMEMBERS myset
1) "chu"
2) "xiao"
3) "wo"
4) "shi"> SMEMBERS myset2
1) "feng"
2) "set2"
Weibo, station B, follow us together! (Intersection)
Number set class:
difference set SDIFF
intersection SINTER
union SUNION> sadd key1 a
(integer) 1> sadd key1 b
(integer) 1> sadd key1 c
(integer) 1> sadd key2 c
(integer) 1> sadd key2 d
(integer) 1> sadd key2 e
(integer) 1> SDIFF key1 key2 //差集
1) "b"
2) "a"> SINTER key1 key2 //交集,共同好友可以实现
1) "c"> sunion key1 key2 //并集
1) "e"
2) "a"
3) "c"
4) "b"
5) "d"

Weibo, User A puts all the people he follows in a set collection! Puts its fans in a set too!
Common attention, common hobbies, second-degree friends, recommended friends! | (Six-degree segmentation theory)

Detailed Hash Collection

Map collection! key-map! At this time, the value is a map set! There is not much difference between the essence and the string, it is still a simple key-value
set myhash field> hset myhash field1 "chufeng"//set一个具体的key-value
(integer) 1> hget myhash field1 //获取一个字段值
"chufeng"> hmset myhash field1 hello field2 xiaoniu//set多个key value
OK> hmget myhash field1 field2//获取多个字段值
1) "hello"
2) "xiaoniu"> hgetall myhash//获取hash中全部的数据
1) "field1"
2) "hello"
3) "field2"
4) "xiaoniu"> hdel myhash field1//删除hash指定的key字段
(integer) 1> hgetall myhash
1) "field2"
2) "xiaoniu"
hlen> hmset myhash field1 hello field2 world
OK> HGETALL mthash
(empty array)> HGETALL myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"> hlen myhash//获取hash表的字段数量
(integer) 2
Determine whether the key in the hash exists> HEXISTS myhash field1//判断hash中的指定key是否存在
(integer) 1> HEXISTS myhash field3
(integer) 0
Only get all the fields and
only get all the values> hkeys myhash//获取hash中所有的key
1) "field2"
2) "field1"> hvals myhash//获取hash中所有的val
1) "world"
2) "hello"
incr dncr> hset myhash field3  5//指定起始增量
(integer) 1> HINCRBY myhash field3 1//自增
(integer) 6> HINCRBY myhash field3 1
(integer) 7> HINCRBY myhash field3 1
(integer) 8> HINCRBY myhash field3 1
(integer) 9> HINCRBY myhash field3 1
(integer) 10> HINCRBY myhash field3 -1//自减
(integer) 9> hsetnx myhash field4 hello//不存在则创建
(integer) 1> hsetnx myhash field4 niuma//如果存在则不能设置
(integer) 0
hset user:1 name chufeng

Hash changed data user name age, especially information that changes frequently, such as user information! Hash is more suitable for object storage, and String is more suitable for string storage!

Detailed explanation of Zset ordered collection

On the basis of set, a value is added, setk1v1 zset k1 score1 v1> zadd myset 1 one//添加一个值
(integer) 1> zadd myset 2 two//添加多个值
(integer) 1> zadd myset 3 three 4 four
(integer) 2> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
4) "four"

排序如何实现> zadd salary 2500 xiaohong//添加三个用户
(integer) 1> zadd salary 5000 zhangsan
(integer) 1> zadd salary 50000 chufeng
(integer) 1
//zrangebyscore key min max> ZRANGEBYSCORE salary -inf +inf//显示全部的用户,从小到大排序
1) "xiaohong"
2) "zhangsan"
3) "chufeng"> ZREVRANGE salary 0 -1//显示全部用户,从大到小排序
1) "chufeng"
2) "zhangsan"> ZRANGEBYSCORE salary -inf +inf withscores//显示全部的用户,并且附带成绩
1) "xiaohong"
2) "2500"
3) "zhangsan"
4) "5000"
5) "chufeng"
6) "50000"> ZRANGEBYSCORE salary -inf 5000 withscores//显示工资小于2500的升序排列
1) "xiaohong"
2) "2500"
3) "zhangsan"
4) "5000"

移除rem中的元素> zrange salary 0 -1
1) "xiaohong"
2) "zhangsan"
3) "chufeng"> zrem salary xiaohong//移除有序集合中的指定元素
(integer) 1> zrange salary 0 -1
1) "zhangsan"
2) "chufeng"> zcard salary//获取有序集合中的个数
(integer) 2> zadd myset 1 hello
(integer) 1> zadd myset 2 world
(integer) 1> zadd myset 3 chufeng 4 niubi
(integer) 2> zcount myset 1 3//获取指定区间的成员数量
(integer) 6> zcount myset 1 6
(integer) 8

The rest of the APIs, through our study, if you need the rest at work, you can check the official documents at this time!
Case idea: set sort,
store class score table, salary table sort!
Common news, 1, Important news 2, judge with weight! The
leaderboard application is implemented, take the Top N test!