Redis installation, startup, basic use of 5 data structures

1. Install and start redis

1. Open the directory
mkdir packages
cd packages/ where you need to download redis
2. Download the redis installation package
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
ls -lrt
3. Unzip the installation package
tar xzfv redis-5.0.3.tar.gz 
cd redis-5.0.3/
ls -lrt
4. Install
make
pwd
ls -lrt
5. Start the redis server
src/redis-server redis.conf &
6. Check that the service is started it
 PS -ef | grep redis
7. the start redis client
SCR / redis-CLI
8. the simple example of the use

[allen@localhost redis-5.0.3]$ src/redis-cli
127.0.0.1:6379> set allen "will win 100 million yuan this year"
OK
127.0.0.1:6379> get allen
"will win 100 million yuan this year"
127.0.0.1:6379> 

How to view official website documents:

On the client side, how to get help documentation:

127.0.0.1:6379> help @set

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0

 

Command introduction (based on function)

1. Single value cache.

2. Object cache

Name Balance
Allen 200000
Bill 4000
Carlyn 30000
David 70000

method one:

 Set user:1 value (Json format data)

Way two:

 mset user:1:Name Allen user:1:Balance 200000

127.0.0.1:6379> mset user:1:Name Allen user:1:Balance 200000
OK
127.0.0.1:6379> mset user:2:Name Bill user:2:Balance 40000
OK
127.0.0.1:6379> mget user:1:Name
1) "Allen"
127.0.0.1:6379> mget user:1:Balance
1) "200000"

3. setNX implements distributed locks

 NX -- Only set the key if it does not already exist.

Return 1 means the setting is successful, return 0 means the setting has failed, the lock is released when the execution is completed and the key is deleted

127.0.0.1:6379> SETNX product:1001 true
(integer) 1
127.0.0.1:6379> SETNX product:1001 true
(integer) 0
127.0.0.1:6379> del product:1001
(integer) 1

4. Use incr to realize the reading function 

127.0.0.1:6379> incr artical:readCount:1001
(integer) 1
127.0.0.1:6379> incr artical:readCount:1001
(integer) 2
127.0.0.1:6379> incr artical:readCount:1001
(integer) 3
127.0.0.1:6379> 

5. Redis hash common operations

Redis hash data structure realizes object storage, Hmset

127.0.0.1:6379> hmset user 1001:name Allen 1001:balence 1000000
OK
127.0.0.1:6379> hmset user 1002:name Sky 1002:balence 10000
OK
127.0.0.1:6379> hgetall user
1) "1001:name"
2) "Allen"
3) "1001:balence"
4) "1000000"
5) "1002:name"
6) "Sky"
7) "1002:balence"
8) "10000"
127.0.0.1:6379> hmget user 1001:name
1) "Allen"
127.0.0.1:6379> 

5.1. Redis hash data structure realizes object storage, Hset

Hash is used to increase the shopping cart scenario:

1) A user id is key-cart:1001

2) Use product id as field-8808

3) Take the quantity of goods as value-1

127.0.0.1:6379> hset cart:1001 8808 1
(integer) 1
127.0.0.1:6379> hincrby cart:1001 8808 1
(integer) 2
127.0.0.1:6379> hlen cart:1001
(integer) 1
127.0.0.1:6379> hget cart:1001 8808
"2"
127.0.0.1:6379> hgetall cart:1001
1) "8808"
2) "2"
127.0.0.1:6379> 

6. List data structure to achieve data structure

6.1 Implementation of stack data structure,

Stack (栈 FILO) = LPUSH + LPOP

127.0.0.1:6379> LPUSH menu "Pai HuangGua"
(integer) 1
127.0.0.1:6379> LPUSH menu "Shao Zhuti"
(integer) 2
127.0.0.1:6379> LPUSH menu "Zheng Xiongzhang"
(integer) 3
127.0.0.1:6379> LPOP menu
"Zheng Xiongzhang"
127.0.0.1:6379> LPOP menu
"Shao Zhuti"
127.0.0.1:6379> 

6.2 Implementation of queue data structure:

Queue (Queue FIFO) = LPUSH + RPOP

127.0.0.1:6379> LPUSH QueueMenu "Pai HuangGua"
(integer) 1
127.0.0.1:6379> LPUSH QueueMenu "Shao ZhuTi"
(integer) 2
127.0.0.1:6379> LPUSH QueueMenu "Zhen XiongZhang"
(integer) 3
127.0.0.1:6379> RPOP QueueMenu
"Pai HuangGua"
127.0.0.1:6379> RPOP QueueMenu
"Shao ZhuTi"
127.0.0.1:6379> 

6.2 Implementation of Blocking Queue Data Structure

Blocking Queue(队列 FIFO) = LPUSH + BRPOP

127.0.0.1:6379> LPUSH QueueMenu "Pai HuangGua"
(integer) 1
127.0.0.1:6379> LPUSH QueueMenu "Shao ZhuTi"
(integer) 2
127.0.0.1:6379> BRPOP QueueMenu 5
1) "QueueMenu"
2) "Pai HuangGua"
127.0.0.1:6379> BRPOP QueueMenu 5
1) "QueueMenu"
2) "Shao ZhuTi"
127.0.0.1:6379> BRPOP QueueMenu 5
(nil)
(5.10s)
127.0.0.1:6379> 

6.3 Use of LRANGE command

127.0.0.1:6379> LPUSH QueueMenu "Zhen XiongZhang"
(integer) 1
127.0.0.1:6379> LPUSH QueueMenu "Shao ZhuTi"
(integer) 2
127.0.0.1:6379> LPUSH QueueMenu "Pai HuangGua"
(integer) 3
127.0.0.1:6379> LPUSH QueueMenu "Chao HuaSheng"
(integer) 4
127.0.0.1:6379> LPUSH QueueMenu "Kao Ya"
(integer) 5
127.0.0.1:6379> LRange QueueMenu 2 4
1) "Pai HuangGua"
2) "Shao ZhuTi"
3) "Zhen XiongZhang"
127.0.0.1:6379> 

7. Set data structure application scenarios

7.1. Lottery, each time the same employee can win multiple times

127.0.0.1:6379> sadd choujinag Allen
(integer) 1
127.0.0.1:6379> sadd choujinag Bill
(integer) 1
127.0.0.1:6379> sadd choujinag Fiona
(integer) 1
127.0.0.1:6379> sadd choujinag Wesley
(integer) 1
127.0.0.1:6379> sadd choujinag Alisa
(integer) 1
127.0.0.1:6379> sadd choujinag Mocha
(integer) 1
127.0.0.1:6379> srandmember choujinag 2
1) "Fiona"
2) "Bill"
127.0.0.1:6379> SMEMBERS choujinag
1) "Wesley"
2) "Mocha"
3) "Alisa"
4) "Fiona"
5) "Bill"
6) "Allen"

7.2 Lottery, the same employee can only win once

127.0.0.1:6379> SPOP choujinag 2
1) "Wesley"
2) "Fiona"
127.0.0.1:6379> SMEMBERS choujinag
1) "Mocha"
2) "Alisa"
3) "Bill"
4) "Allen"
127.0.0.1:6379> 

7.3 Set implements the like function

Like -> Cancel like -> Check whether to like -> View the list of likes -> View the number of likes

127.0.0.1:6379> sadd like:Message1001 Allen
(integer) 1
127.0.0.1:6379> sadd like:Message1001 Bill
(integer) 1
127.0.0.1:6379> sadd like:Message1001 Fiona
(integer) 1
127.0.0.1:6379> sadd like:Message1001 Mocha
(integer) 1
127.0.0.1:6379> srem like:Message1001 Fiona
(integer) 1
127.0.0.1:6379> SISMEMBER like:Message1001 Bill
(integer) 1
127.0.0.1:6379> SISMEMBER like:Message1001 Fiona
(integer) 0
127.0.0.1:6379> SMEMBERS like:Message1001
1) "Bill"
2) "Allen"
3) "Mocha"
127.0.0.1:6379> scard like:Message1001
(integer) 3
127.0.0.1:6379> 

7.4 Set operations

Union - SINTER

Or set - SUNION

Difference set --SDIFF-Subtract all subsequent sets from the first set

127.0.0.1:6379> sadd fruit1 apple banana
(integer) 2 
127.0.0.1:6379> sadd fruit2 pear berry grape apple orange
(integer) 2
127.0.0.1:6379> sadd fruit3 apple mongo
(integer) 2
127.0.0.1:6379> SINTER fruit1 fruit2
1) "apple"
127.0.0.1:6379> SUNION fruit3 fruit1
1) "banana"
2) "apple"
3) "mongo"
127.0.0.1:6379> sdiff fruit1 fruit2
1) "banana"
127.0.0.1:6379> 

Filtering solutions on e-commerce websites:

8. Ordered set zset

zset operation command


Simple use of the above command:

127.0.0.1:6379> zadd MathScore 90 allen 80 bill 65 mocha 85 fiona
(integer) 4
127.0.0.1:6379> ZREM MathScore mocha
(integer) 1
127.0.0.1:6379> zscore MathScore mocha
(nil)
127.0.0.1:6379> zscore MathScore fiona
"85"
127.0.0.1:6379> zcard MathScore 
(integer) 3
127.0.0.1:6379> ZRANGE MathScore 1 2
1) "fiona"
2) "allen"
127.0.0.1:6379> ZRANGE MathScore 1 2 withscores
1) "fiona"
2) "85"
3) "allen"
4) "90"
127.0.0.1:6379> ZREVRANGE MathScore 0 2
1) "allen"
2) "fiona"
3) "bill"
127.0.0.1:6379> zadd EnglishScore 100 allen 80 sky 70 kevin
(integer) 3
127.0.0.1:6379> ZINCRBY MatchScore 1 fiona
"86"
127.0.0.1:6379> ZINCRBY MatchScore 1 fiona
"87"
127.0.0.1:6379> ZINCRBY MatchScore 1 fiona
"88"
127.0.0.1:6379> ZUNIONSTORE Class061Score 2 MatchScore EnglishScore
(integer) 5
127.0.0.1:6379> ZREVRANGE Class061Score 0 4 withscores
 1) "allen"
 2) "190"
 3) "fiona"
 4) "88"
 5) "sky"
 6) "80"
 7) "bill"
 8) "80"
 9) "kevin"
10) "70"

9. Other advanced commands, list all keys

keys *

keys allen

127.0.0.1:6379> keys *
 1) "menu"
 2) "user:1:Balance"
 3) "user:1:Name"
 4) "QueueMenu"
 5) "fruit1"
 6) "choujinag"
 7) "EnglishScore"
 8) "artical:readCount:1001"
 9) "fruit3"
10) "Class061Score"
11) "user:2:Balance"
12) "cart:1001"
13) "like:Message1001"
14) "MatchScore"
15) "fruit2"
16) "user"
17) "user:2:Name"
18) "allen"
127.0.0.1:6379> keys allen
1) "allen"

Use keys to scan the entire redis, which will cause block

10. Progressive traversal, scanning until the returned value is cursor 0.

The cursor here is the table value of the bucket in the hash map.

scan 0 match user* count 3

127.0.0.1:6379> keys user*
1) "user:1:Balance"
2) "user:1:Name"
3) "user1"
4) "user:2:Balance"
5) "user3"
6) "user2"
7) "user"
8) "user:2:Name"
127.0.0.1:6379> scan 0 match user* count 3
1) "20"
2) 1) "user:1:Name"
127.0.0.1:6379> scan 20 match user* count 3
1) "28"
2) 1) "user:2:Balance"
127.0.0.1:6379> scan 28 match user* count 3
1) "30"
2) 1) "user1"
127.0.0.1:6379> scan 30 match user* count 3
1) "13"
2) 1) "user"
   2) "user:2:Name"
   3) "user3"
127.0.0.1:6379> scan 13 match user* count 3
1) "19"
2) 1) "user:1:Balance"
127.0.0.1:6379> scan 19 match user* count 3
1) "31"
2) 1) "user2"
127.0.0.1:6379> scan 31 match user* count 3
1) "0"
2) (empty list or set)
127.0.0.1:6379> 

 

Guess you like

Origin blog.csdn.net/pengweismile/article/details/112093565