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

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

String string details

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

Object

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

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"
getset #get in set first
127.0.0.1:6379> getset db  "redis"//如果不存在,则返回uil
(nil)
127.0.0.1:6379> getset db "mysql"//如果存在值,获取原来的值,在去设置新的值
"redis"
127.0.0.1:6379> get db
"mysql"

The data structure is the same

String similar usage scenarios: value can be our number in addition to our string!
●Counter.
●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

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//获取list中的所有值
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"
LPOP
RPOP
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> Lpop list//移除list 的第一个元素
"three"
127.0.0.1:6379> RPOP list//移除list的最后一个元素
"right"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
Lindex gets the value by subscript
127.0.0.1:6379> Lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> Lindex list 0//通过下标获取值
"two"
127.0.0.1:6379> Lindex list 1
"one"
Llen list length
127.0.0.1:6379> Llen list//返回列表的长度
(integer) 2
Remove the specified value
127.0.0.1:6379> LPUSH list two
(integer) 3
127.0.0.1:6379> lrem list 1 one//移除list集合中指定个数的value,精确匹配
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "two"
127.0.0.1:6379> lrem list 2 two
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
(empty array)
trim: list truncation
127.0.0.1:6379> RPUSH mylist "hello gouzi"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello zhuzi"
(integer) 2
127.0.0.1:6379> RPUSH mylist "hello niuzi"
(integer) 3
127.0.0.1:6379> RPUSH mylist "hello mazi"
(integer) 4
127.0.0.1:6379> ltrim mylist 1 2//通过下标截取指定的长度,这个list已经被改变了,只剩下截取的元素
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "hello zhuzi"
2) "hello niuzi"
rpoplpush#Remove the last element of the list and add it to another list
127.0.0.1:6379> rpush mylist 'woaini'
(integer) 1
127.0.0.1:6379> rpush mylist 'woaini1'
(integer) 2
127.0.0.1:6379> rpush mylist 'woaini2'
(integer) 3
127.0.0.1:6379> rpoplpush mylist myotherlist//移除列表的最后一个元素,并添加到另外一个列表中
"woaini2"
127.0.0.1:6379> lrange mylist 0 -1//查看原来的列表
1) "woaini"
2) "woaini1"
127.0.0.1:6379> lrange myotherlist 0 -1//查看目标列表
1) "woaini2"
lset: replace the value of the specified subscript in the list, update operation
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 values1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "values1"
127.0.0.1:6379> lset list 0 item//如果存在会替换当前下标的值
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
Linsert: Insert a specific value before or after an element
127.0.0.1:6379> rpush mylist "hello"
(integer) 1
127.0.0.1:6379> rpush mylist "world"
(integer) 2
127.0.0.1:6379> LINSERT MYLIST BEFORE "world" "other"
(integer) 0
127.0.0.1:6379> LINSERT mylist BEFORE "world" "other"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
127.0.0.1:6379> LINSERT mylist after "world" "new"
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"

summary:

●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

127.0.0.1:6379> sadd myset "hello"//set集合中添加一个元素
(integer) 1
127.0.0.1:6379> sadd myset "chufeng"
(integer) 1
127.0.0.1:6379> sadd myset "lovechufeng"
(integer) 1
127.0.0.1:6379> SMEMBERS myset//查看set中的所有指定元素
1) "chufeng"
2) "hello"
3) "lovechufeng"
127.0.0.1:6379> SISMEMBER myset hello//判断某一个值是否在set集合中
(integer) 1
127.0.0.1:6379> SISMEMBER myset map
(integer) 0

127.0.0.1:6379> scard myset//获取set集合中的元素个数
(integer) 3

127.0.0.1:6379> srem myset hello//移除set集合中的指定元素
(integer) 1
127.0.0.1:6379> smembers myset
1) "chufeng"
2) "lovechufeng"
set Unordered non-repeating collection. Random!
127.0.0.1:6379> SRANDMEMBER myset//随机抽选出一个元素
"lovechufeng2"
127.0.0.1:6379> SRANDMEMBER myset
"chufeng"
127.0.0.1:6379> SRANDMEMBER myset
"chufeng"
127.0.0.1:6379> SRANDMEMBER myset
"lovechufeng"
127.0.0.1:6379> SRANDMEMBER myset
"lovechufeng"
127.0.0.1:6379> SRANDMEMBER myset
"lovechufeng2"
127.0.0.1:6379> SRANDMEMBER myset
"chufeng"
127.0.0.1:6379> SRANDMEMBER myset 2//随机抽出指定个数的元素
1) "chufeng"
2) "lovechufeng2"
Delete the specified key, delete the key randomly!
127.0.0.1:6379> auth 123456//输入redis密码
OK
127.0.0.1:6379> SMEMBERS myset
1) "lovechufeng2"
2) "chufeng"
3) "lovechufeng"
127.0.0.1:6379> spop myset//随机移除元素
"lovechufeng2"
127.0.0.1:6379> spop myset
"lovechufeng"
127.0.0.1:6379> SMEMBERS myset
1) "chufeng"
Move a specified value to another set collection
127.0.0.1:6379> sadd myset "wo"
(integer) 1
127.0.0.1:6379> sadd myset "shi"
(integer) 1
127.0.0.1:6379> sadd myset "xiao"
(integer) 1
127.0.0.1:6379> sadd myset "chu"
(integer) 1
127.0.0.1:6379> sadd myset "feng"
(integer) 1
127.0.0.1:6379> sadd myset2 "set2"
(integer) 1
127.0.0.1:6379> smove myset myset2 "feng"//将set集合中的指定元素移动到另一个集合中
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "chu"
2) "xiao"
3) "wo"
4) "shi"
127.0.0.1:6379> SMEMBERS myset2
1) "feng"
2) "set2"
Weibo, station B, follow us together! (Intersection)
Number set class:
difference set SDIFF
intersection SINTER
union SUNION
127.0.0.1:6379> sadd key1 a
(integer) 1
127.0.0.1:6379> sadd key1 b
(integer) 1
127.0.0.1:6379> sadd key1 c
(integer) 1
127.0.0.1:6379> sadd key2 c
(integer) 1
127.0.0.1:6379> sadd key2 d
(integer) 1
127.0.0.1:6379> sadd key2 e
(integer) 1
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) "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

127.0.0.1:6379> hset myhash field1 "chufeng"//set一个具体的key-value
(integer) 1
127.0.0.1:6379> hget myhash field1 //获取一个字段值
"chufeng" 
127.0.0.1:6379> hmset myhash field1 hello field2 xiaoniu//set多个key value
OK
127.0.0.1:6379> hmget myhash field1 field2//获取多个字段值
1) "hello"
2) "xiaoniu"
127.0.0.1:6379> hgetall myhash//获取hash中全部的数据
1) "field1"
2) "hello"
3) "field2"
4) "xiaoniu"
127.0.0.1:6379> hdel myhash field1//删除hash指定的key字段
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "xiaoniu"
hlen
127.0.0.1:6379> hmset myhash field1 hello field2 world
OK
127.0.0.1:6379> HGETALL mthash
(empty array)
127.0.0.1:6379> HGETALL myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379> hlen myhash//获取hash表的字段数量
(integer) 2
Determine whether the key in the hash exists
127.0.0.1:6379> HEXISTS myhash field1//判断hash中的指定key是否存在
(integer) 1
127.0.0.1:6379> HEXISTS myhash field3
(integer) 0
Only get all the fields and
only get all the values
127.0.0.1:6379> hkeys myhash//获取hash中所有的key
1) "field2"
2) "field1"
127.0.0.1:6379> hvals myhash//获取hash中所有的val
1) "world"
2) "hello"
incr dncr
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) 7
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 8
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 9
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 10
127.0.0.1:6379> HINCRBY myhash field3 -1//自减
(integer) 9

127.0.0.1:6379> hsetnx myhash field4 hello//不存在则创建
(integer) 1
127.0.0.1:6379> 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
127.0.0.1:6379> zadd myset 1 one//添加一个值
(integer) 1
127.0.0.1:6379> zadd myset 2 two//添加多个值
(integer) 1
127.0.0.1:6379> zadd myset 3 three 4 four
(integer) 2
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
4) "four"

排序如何实现

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

移除rem中的元素

127.0.0.1:6379> zrange salary 0 -1
1) "xiaohong"
2) "zhangsan"
3) "chufeng"
127.0.0.1:6379> zrem salary xiaohong//移除有序集合中的指定元素
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
2) "chufeng"
127.0.0.1:6379> zcard salary//获取有序集合中的个数
(integer) 2
127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world
(integer) 1
127.0.0.1:6379> zadd myset 3 chufeng 4 niubi
(integer) 2
127.0.0.1:6379> zcount myset 1 3//获取指定区间的成员数量
(integer) 6
127.0.0.1:6379> 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!