Redis Advanced - Advanced real core data structure

Here Insert Picture Description


getting Started

If you have not been exposed Redis, please read [] entry to the master Redis-

Here Insert Picture Description


The basic data structure Redis

Here Insert Picture Description

Here we briefly review the case, remember that common enough, remember the visit the official website.

Redis API

Spoken API

  keys 、del 、dbsize 、expire (ttl、persist)、exists、type…….

Time complexity is O (1)


strings\ hash\list\set\zset

Strings

SET  key  value 			//存入字符串键值对
MSET  key  value [key value ...] 	//批量存储字符串键值对
SETNX  key  value 		//存入一个不存在的字符串键值对
GET  key 			//获取一个字符串键值
MGET  key  [key ...]	 	//批量获取字符串键值
DEL  key  [key ...] 		//删除一个键
EXPIRE  key  seconds 		//设置一个键的过期时间(秒)

原子加减
INCR  key 			//将key中储存的数字值加1
DECR  key 			//将key中储存的数字值减1
INCRBY  key  increment 		//将key所储存的值加上increment
DECRBY  key  decrement 	//将key所储存的值减去decrement

Hash

HSET  key  field  value 			//存储一个哈希表key的键值
HSETNX  key  field  value 		//存储一个不存在的哈希表key的键值
HMSET  key  field  value [field value ...] 	//在一个哈希表key中存储多个键值对
HGET  key  field 				//获取哈希表key对应的field键值
HMGET  key  field  [field ...] 		//批量获取哈希表key中多个field键值
HDEL  key  field  [field ...] 		//删除哈希表key中的field键值
HLEN  key				//返回哈希表key中field的数量
HGETALL  key				//返回哈希表key中所有的键值
HINCRBY  key  field  increment 		//为哈希表key中field键的值加上增量increment


List

LPUSH  key  value [value ...] 		//将一个或多个值value插入到key列表的表头(最左边)
RPUSH  key  value [value ...]	 	//将一个或多个值value插入到key列表的表尾(最右边)
LPOP  key			//移除并返回key列表的头元素
RPOP  key			//移除并返回key列表的尾元素
LRANGE  key  start  stop		//返回列表key中指定区间内的元素,区间以偏移量start和stop指定
BLPOP  key  [key ...]  timeout	//从key列表表头弹出一个元素,若列表中没有元素,阻塞等待					timeout秒,如果timeout=0,一直阻塞等待
BRPOP  key  [key ...]  timeout 	//从key列表表尾弹出一个元素,若列表中没有元素,阻塞等待					timeout秒,如果timeout=0,一直阻塞等待

Here Insert Picture Description


Set

Set常用操作
SADD  key  member  [member ...]			//往集合key中存入元素,元素存在则忽略,
							若key不存在则新建
SREM  key  member  [member ...]			//从集合key中删除元素
SMEMBERS  key					//获取集合key中所有元素
SCARD  key					//获取集合key的元素个数
SISMEMBER  key  member			//判断member元素是否存在于集合key中
SRANDMEMBER  key  [count]			//从集合key中选出count个元素,元素不从key中删除
SPOP  key  [count]				//从集合key中选出count个元素,元素从key中删除

Set运算操作
SINTER  key  [key ...] 				//交集运算
SINTERSTORE  destination  key  [key ..]		//将交集结果存入新集合destination中
SUNION  key  [key ..] 				//并集运算
SUNIONSTORE  destination  key  [key ...]		//将并集结果存入新集合destination中
SDIFF  key  [key ...] 				//差集运算
SDIFFSTORE  destination  key  [key ...]		//将差集结果存入新集合destination中

token

  ZSet常用操作
ZADD key score member [[score member]]	//往有序集合key中加入带分值元素
ZREM key member [member …]		//从有序集合key中删除元素
ZSCORE key member 			//返回有序集合key中元素member的分值
ZINCRBY key increment member		//为有序集合key中元素member的分值加上increment 
ZCARD key				//返回有序集合key中元素个数
ZRANGE key start stop [WITHSCORES]	//正序获取有序集合key从start下标到stop下标的元素
ZREVRANGE key start stop [WITHSCORES]	//倒序获取有序集合key从start下标到stop下标的元素

Zset集合操作
ZUNIONSTORE destkey numkeys key [key ...] 	//并集计算
ZINTERSTORE destkey numkeys key [key …]	//交集计算

Here Insert Picture Description


Scenarios

Strings

Single-value cache

SET  key  value 	
GET  key 	

Object Caching

1) SET  user:1  value (json格式数据)
2) MSET  user:1:name  artisan user:1:balance  1888
  MGET  user:1:name  user:1:balance 

Distributed Lock

SETNX  product:10001  true 		//返回1代表获取锁成功
SETNX  product:10001  true 		//返回0代表获取锁失败
。。。执行业务操作
DEL  product:10001			//执行完业务释放锁
SET product:10001 true  ex  10  nx	//防止程序意外终止导致死锁

counter

INCR article:judge:{文章id}  	
GET article: judge:{文章id} 

Here Insert Picture Description

Forwarding comment

How to cluster environments concurrently access the same article guarantee thread safety

redis threaded atomicity


Web cluster shared session

spring session + redis achieve shared session

spring session itself is to use as an external storage redis


Global Distributed System serial number

INCRBY  orderId  1000

Every time get 1000 into memory, VS are going to take every one redis

Multi-table N, N multiple concurrent, N multi-scene using redis

Node hung it does not matter, nothing more than a waste of id


Hash

Object Caching

HMSET  user  {userId}:name  artisan  {userId}:balance  1888
HMSET  user  1:name  artisan  1:balance  1888
HMGET  user  1:name  1:balance  

Timeout for the key, not for the field
to avoid the big key


Electricity supplier shopping cart

1) the user id to Key
2) for the item id of Field
. 3) the number of items of value

• Operating cart

  1. Add product hset cart:1001 10088 1
  2. Increase the number hincrby cart:1001 10088 1
  3. The total number of goods hlen cart:1001
  4. Delete item hdel cart:1001 10088
  5. Gets cart all goods hgetall cart:1001

Here Insert Picture Description


List

Common data structures

Stack() = LPUSH + LPOP  FILO
Queue(队列)= LPUSH + RPOP
Blocking MQ(阻塞队列)= LPUSH + BRPOP ( 会阻塞其他消息,如果使用,使用单独的Redis)

Micro channel - subscribe message number

Here Insert Picture Description

For example, I am concerned about the public number, the message sent by these numbers how public display?

For example
1) A 23:00 No release public article, the message ID is 10018

LPUSH  msg:{我的-ID}  10018

2) B 23:05 article published public number, the message ID is 10086

LPUSH  msg:{我的-ID} 10086

3) C 23:30 article published public number, the message ID is 10099

LPUSH  msg:{我的-ID} 10099

3) Check the latest concern of the public announcement No.

LRANGE  msg:{我的-ID}  0  5

I have to put this queue, lpush release from the left

C article article id id A B of article id

Get, certainly in chronological flashback row

LRANGE  msg:{我的-ID}  start  stop 

Set

Thumbs up, collection, label

  1. like
SADD  like:{消息ID}  {用户ID}
  1. Cancel thumbs up
SREM like:{消息ID}  {用户ID}
  1. Check whether the user is past Chan
SISMEMBER  like:{消息ID}  {用户ID}
  1. Users get a list of thumbs up
SMEMBERS like:{消息ID}
  1. Users get thumbs up
SCARD like:{消息ID}

lottery

Click added lottery participation set
the SADD userlD} {Key
2) See all users participate in the lottery
SMEMBERS Key
. 3) were extracted count winners
SRANDMEMBER key [count] / SPOP key [count]

One two three prize draw Removes SPOP

Here Insert Picture Description


Common interest, mutual friend. . .

Here Insert Picture DescriptionApplication collection operation

SINTER set1 set2 set3 -> { c }
SUNION set1 set2 set3 ->  { a,b,c,d,e }
SDIFF set1 set2 set3 ->  { a }   以第一个集合为基准,减去后面的集合所有元素
             Abc -  bcd  cde --->  a  第一个集合不存在的不算

token

Trending

Here Insert Picture Description

1) Click on News

ZINCRBY  hotNews:20200307  1  fujian 【score加1

2) shows the date of the top ten ranking

ZREVRANGE  hotNews:20200307  0  10  WITHSCORES 

3) calculate the seven search list

ZUNIONSTORE  hotNews:20200301-20200307  7 
hotNews:20200301  hotNews:202000302... hotNews:202000307

4) shows seven top ten ranking

ZREVRANGE hotNews:20200301-20200307  0  10  WITHSCORES
Published 825 original articles · won praise 2060 · Views 4.2 million +

Guess you like

Origin blog.csdn.net/yangshangwei/article/details/104711721