“This is the 24th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

This article is mainly about Redis five basic types, more commands and practices.

The application of redis traditional 5 data types

Redis traditional 5 big data type landing application

Redis is introduced:

Redis is an open source (BSD-licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures, such as strings (hashes), lists (Lists), sets (sets), sorted sets (sorted sets)] and range queries, bitmaps, Hyperloglogs and Geospatial index radius query. Redis is built with replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, And provides high availability through Redis Sentinel and Automated partitioning (Cluster).

Redis command: www.redis.cn/commands.ht…

Note: The redis command is case insensitive, while the key is case sensitive

Query command help:

Help @ type noun

Example:

String String type

The most commonly used

set key vuue

getkey

Sets/gets multiple key values simultaneously

MSET key value [key value …]

MGET key [key ,,,]

Numerical increase or decrease

Incremental number INCR key

Increments the specified integer incrby key increment

Decr key

Decreases the specified integer decrby key Decrement

Get character length

STRLEN key

A distributed lock

setnx key value

set key value [EX seconds] [PX milliseconds] [NX|XX]

  • EX: The number of seconds after the key expires
  • PX: How many milliseconds after the key expires
  • NX: creates a key when it does not exist. The effect is equivalent to setnx
  • XX: Overwrites the key if it exists

Using the scenario

  • The product number and order number are generated using the INCR command
  • Whether or not you like the article

Read number: Once the rest address is clicked, add a number 1 using the incr key command to finish recording the number.

Command practice

Hash Hash type

Mapping to Java data structures

Map<Stirng, Map<Object, Object>>

Set one field value at a time

HSET key field value

Get one field value at a time

HGET key field

Set multiple field values at once

HMSET key field value [fild value …]

Get multiple field values at once

HMGET key field [field …]

Gets all field values

hgetall key

Gets all quantities within a key

hlen

Delete a key

hdel

Command shows

Application scenarios

Shopping cart early, current medium – sized factory can use

New Product –> hSET ShopCar: UID1024 334488 1

New Product –> hSET ShopCar: UID2014 334477 1

Add product quantity –> Hincrby Shopcar: UID1024 334477 1

Total number of goods –> hlen Shopcar: UID1024

All options –> HGETall ShopCar: UID1024

List List type

Add an element to the left of the list

lpush key value [value …]

Add elements to the right of the list

rpush key value [value …]

Check the list

lrange key start stop

Gets the number of elements in the list

llen key

Command to use

Application scenarios

Wechat article subscription public account

1. [XX Bao] and [XX Newspaper] published articles 11 and 22 respectively

2. The author follows them and pushes them to my list as long as they publish new articles

lpush likearticle: uid1024 11 22

Lrange likeArticle: uID1024 0 10 lrange likeArticle: uID1024 0 10

Set does not duplicate list types

Add elements

sadd key member [member …]

Remove elements

srem key member [member …]

Gets all elements of the collection

smembers key

Determines whether an element is in a collection

sismember key member

Gets the number of elements in the collection

scard key

Pops a random element from the collection, element not deleted

Srandmember key [number]

Pop up a random element from the set and delete one

Spop key [number]

Set operations

The difference set operation on the set A minus B

The set of elements that belong to A but not B

sdiff key [key …]

The intersection element of the set is A ^ B

That’s part of both A and B

simter key [key …]

The union operation on sets A, V, B

The set of merged elements that belong to A or B

sunion key [key …]

Using the scenario

Wechat lucky draw small program

1, user ID, participate now, sadd key user ID

2, show how many people have participated in the current 67,231 people participated, scard key

3, lucky draw (randomly select N winners from set)

Srandmember Key 2 Random draw 2 people, elements will not be deleted

Spop Key 3 drivers raffle 3 people, elements will be removed

Wechat circle of friends like

1. Add likes; Sadd pub:msgid Indicates user ID1 indicates user ID2

2. Cancel “like”; Srem pub:msgid ID of the like user

Smembers pub:msgid smembers pub:msgid

4, the statistics of the number of “like” users, is the common red number to be liked scard pub:msgid

Sismember pub:msgid user ID

Micro blog friends follow social relationships

People of common concern

sadd s1 1 2 3 4

sadd s2 2 3 6 8

sinter s1 s2

Co-following: I go to someone’s Weibo and immediately get the people that I share with them

The people I follow, I follow him.

I followed Yu Chengdong of Huawei, who also paid attention to Zhang Zhaodong. I have the same hobby with Yu

sadd s1 1 2 3 4 5

sadd s2 3 4 5 6 7

sismember s1 3

sismember s2 3

QQ push people you may know

sadd s1 1 2 3 4 5

sadd s2 3 4 5 6 7

// Mutual friends

sinter s1 s2

/ / difference set

sdiff s1 s2

sdiff s2 s1

Zset ordered set

Common commands

1. Add an element and its fraction to the ordered collection

2. Add elements

  • ZADD key score member [score member …]

Returns all elements in the index from strat to stop in the order in which the element score arrives

  • zrange key start stop [WITHSORES]

4. Get the score of the element

  • zscore key member [member …]

5. Delete elements

  • zrem key member [member …]

Gets the element in the specified score range

  • zrangebyscore key min max [WITHSCORES] [LIMIT offset count]

7. Increase the score of an element

  • zincrby key increment member

Get the number of elements in the collection

  • zcard key

9. Get the number of elements within the specified score range

  • zcount key min max

10. Delete elements by rank range

  • zremrangebyrank key start stop

Get the ranking of elements

  • Zrank Key member from childhood to adulthood

  • Zrevrank key member from large to small

Application scenarios

1. Display goods in order of sales

Given a sorted set, we select goods: Sellsort as the key and the number of items sold as the score.

Zadd Goods: Sellsort 9 1001 15 1002 | Zadd Goods: Sellsort 9 1001 15 1002

There is a customer who buys 2 more goods 1001, the number of goods is 1001 resounding increase 2 | Zincrby goods: Sellsort 2 10001

Zrange goods: Sellsort 0 10 withscores

2. Google Docs

1. Click the video

ZINCRBY Hotavi :20220203 1 800

ZINCRBY Hotavi :20220203 15 800 2 Mulan

2. Display the top 10 items of the day

zrevrange hotavi:20220203 0 9 withscores

The resources

  • redis.io