This is the second day of my participation in the More text Challenge. For more details, see more text Challenge

This article is participating in the “Java Theme Month – Java Development in action”. See the link to the event for more details


Related articles

Redis combat summary: Redis combat

Connect to a Redis five data types SAO operation ①–Redis actual combat (I) | Java development actual combat click jump


Five data types of Redis

  1. String(字符串)-传送门.
  2. List (List)

Lpush, lrange, and rpush operations

#lpush
127.0. 01.:6379> lpush listV1 # add a new set (integer)1
127.0. 01.:6379> lpush list v2
(integer)2 127.0.0.1:6379 > lpushlist v3
(integer) 3
#lrange127.0.0.1:6379 > LRANGElist0 -1 # querylist1) "v3" 2) "v2" 3) "v1" 127.0.0.1:6379> lpush list1 v1 v2 V3 V4 V5 #(integer)5 127.0.0.1:6379> LRANGE list1 0-1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" l--left#rpush127.0.0.1:6379 > LRANGElist1) "v3" 2) "v2" 127.0.0.1:6379> LRANGElist1) "v3" 127.0.0.1:6379> rpushlistRv0 # right insert, as opposed to lpush, where the element is added at the end!(integer)4 127.0.0.1:6379 > lrangelist1) "v3" 2) "v2" 3) "v1" 4) "rv0" We use lpush every time, and the old data is always behind. Is it equivalent to updating the data operation, but the data record is still there? All elements of the collection can be retrieved if you want to query records!Copy the code

② LPOP (left remove) and RPOP (right remove) operations

#lpop
127.0. 01.:6379> LRANGE list 0 - 1
1) "v5"
2) "v4"
3) "v3"
4) "v2"
5) "v1"
127.0. 01.:6379> lpop list# Remove the first element from the header"v5"# # # # # # # # # # # # # # # # # ##rpop
127.0. 01.:6379> LRANGE list 0 - 1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0. 01.:6379> rpop list
"v1"
127.0. 01.:6379> LRANGE list 0 - 1# Remove the first element from the tail1) "v4"
2) "v3"
3) "v2"
Copy the code

③ Lindex (query the specified subscript element), llEN (obtain the set length) operations

#lindex
127.0. 01.:6379> LRANGE list 0 - 1
1) "v4"
2) "v3"
3) "v2"
127.0. 01.:6379> lindex list 1# Get the elements from the set with the specified subscript position0Start counting"v3"
127.0. 01.:6379> lindex list 0# Is equivalent to Java indexof"v4"
#llen
127.0. 01.:6379> llen listGet the length of the elements in the specified collection, which is equivalent to Java's length or size (integer)3

Copy the code

Lrem (remove the specified value based on value)

127.0. 01.:6379> LRANGE list 0 - 1
1) "v4"
2) "v3"
3) "v2"
127.0. 01.:6379> lrem list 1V2 # Remove collectionlistThe elements in are members of v21(integer)1
127.0. 01.:6379> LRANGE list 0 - 1
1) "v4"
2) "v3"
127.0. 01.:6379> lrem list 0V3 # remove collectionlistThe elements in are members of v21One, right here0and1The effect is consistent (INTEGER)1
127.0. 01.:6379> LRANGE list 0 - 1
1) "v4"
127.0. 01.:6379> lpush list  v3 v2 v2 v2
(integer)4 127.0.0.1:6379 > LRANGElist0 and 1, 1) "v2" 2) "v2" 3) "v2" 4) "v3" 5) "v4" 127.0.0.1:6379 > lremlist3 v2 # Remove collectionlistIf the actual number of elements in the collection is not up to the standard, no error will be reported. After all elements are removed, the number of successfully removed elements will be returned(integer)3 127.0.0.1:6379 > LRANGElist 0 -1
1) "v3"
2) "v4"
Copy the code

⑥ltrim and rpoplpush operations (remove the last element from a specified collection into a new one)

#ltrim
127.0. 01.:6379> lpush list v1 v2 v3 v4
(integer)4 127.0.0.1:6379 > LRANGElist1 0 and 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379 > ltrimlistOne, two, and I'm going to take the specified length by subscript, this onelistHas been changed, leaving only the truncated element that we specified as OK 127.0.0.1:6379> LRANGElist 0 -1
1) "v3"
2) "v2"
################
#rpoplpush127.0.0.1:6379 > lpushlist v1 v2 v3 v4 v5
(integer)5 127.0.0.1:6379 > LRANGElist0 and 1, 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379 > rpoplpushlistNewlist # removelistThe last element in the collection is added to the new collection newList, and the return value is the last element value removed "v1" 127.0.0.1:6379> LRANGElist0-1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 127.0.0.1:6379> LRANGE newList 0-1 #Copy the code

⑦ Lset (update) and linsert operations

#lset
127.0. 01.:6379> LRANGE list 0 - 1
1) "v5"
2) "v4"
3) "v3"
4) "v2"
127.0. 01.:6379> 
127.0. 01.:6379> lset list 1NewV5 # updatelistThe subscript in the set is'1'is' newV5' OK127.0. 01.:6379> LRANGE list 0 - 1# Verify that the update was successful1) "v5"
2) "newV5"
3) "v3"
4) "v2"## Note:127.0. 01.:6379> lset list1 0VVVV # Error ERR no such key if the specified 'set' does not exist127.0. 01.:6379> lset list 8VVV # if the collection, but the specified 'subscript' was not found, an error (error) ERR index out of range # # # # # # # # # # # # # # # # # # # # # # # ##linsert
127.0. 01.:6379> LRANGE list 0 - 1
1) "v5"
2) "newV5"
3) "v3"
4) "v2"
127.0. 01.:6379> LINSERT listAfter v3 insertv3 # add an element (integer) after the 'v3' element in the set5
127.0. 01.:6379> LRANGE list 0 - 1
1) "v5"
2) "newV5"
3) "v3"
4) "insertv3"
5) "v2"
127.0. 01.:6379> LINSERT listBefore v3 insertv3 # add an element (integer) before the element (v3) in the set6
127.0. 01.:6379> LRANGE list 0 - 1
1) "v5"
2) "newV5"
3) "insertv3"
4) "v3"
5) "insertv3"
6) "v2"
Copy the code

End summary:

  • It’s actually a linked list where before Node after, left, and right can be inserted
  • If the key does not exist, create a new list
  • If the key exists, add the key
  • If all values are removed, the list is empty, which also means it does not exist!
  • Insert or change values on both sides, the most efficient! The middle element is a little less efficient
  • Message queue! Message queue (Lpush Rpop), stack (Lpush Lpop)!
  1. The Set element is uniquely unique

Sadd (add), smembers (see all elements), sisMember (see if there is one), scard (see length), sREM (remove element) operations

All elements in a set are unique!
127.0. 01.:6379> sadd set1 ding da mian Tiao #setSet (can be batch or single, written in the same way, not to be repeated) (INTEGER)4
127.0. 01.:6379> SMEMBERS set1 #setAll elements in1) "mian"
2) "da"
3) "tiao"
4) "ding"
127.0. 01.:6379> SISMEMBER set1 da #setIn, in return1
(integer) 1
127.0. 01.:6379> SISMEMBER set1 da1 # is not returned0
(integer) 0
127.0. 01.:6379> SCARD set1 # Select size, length (integer)4
127.0. 01.:6379> srem set1 da # removesetThe element specified in (INTEGER)1
127.0. 01.:6379> SMEMBERS set1 #1) "mian"
2) "tiao"
3) "ding"
Copy the code

(2) SrandMember operation

127.0. 01.:6379> sadd myset 1 2 3 4 5 6 7# insetadd7Elements (an integer)7
127.0. 01.:6379> SMEMBERS myset
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "Seven"
127.0. 01.:6379> SRANDMEMBER myset 1# Randomly select from mySet1Return elements of1) "4"
127.0. 01.:6379> SRANDMEMBER myset 1# Randomly select from mySet1Return elements of1) "1"
127.0. 01.:6379> SRANDMEMBER myset 1# Randomly select from mySet1Return elements of1) "5"
127.0. 01.:6379> SRANDMEMBER myset1, but the following return does not have an ordinal value"3"
127.0. 01.:6379> SRANDMEMBER myset 3# Randomly select from mySet3Return elements of1) "1"
2) "2"
3) "3"
127.0. 01.:6379> SRANDMEMBER myset 3# Randomly select from mySet3Return elements of1) "6"
2) "3"
3) "5"
Copy the code

(3) Spop (random removal of elements), smove (movement of specified elements into a new set) operations

127.0. 01.:6379> SMEMBERS myset
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "Seven"
127.0. 01.:6379> < p style = "text-align: center1Is deleted without specifying a parameter value1a"2"
127.0. 01.:6379> spop myset 1# Random delete1An element1) "Seven"
127.0. 01.:6379> spop myset 2# Random delete2An element1) "3"
2) "5"
127.0. 01.:6379> SMEMBERS myset1) "1"
2) "4"
3) "6"
127.0. 01.:6379> smove myset myset2 1# Move specifysetThe specified element in the newset(integer)1
127.0. 01.:6379> SMEMBERS mysetsetA collection of1) "4"
2) "6"
127.0. 01.:6379> SMEMBERS myset2 #setSet, if the newsetIf it exists, it will be added. If it does not exist, it will be created automaticallysetAnd join in1) "1"
Copy the code

(4) Sdiff, sinter, sunion operations

127.0. 01.:6379> sadd myset1 1 2 3 4 5
(integer) 5
127.0. 01.:6379> sadd myset2 3 4 5 6 7
(integer) 5
127.0. 01.:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0. 01.:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "Seven"
127.0. 01.:6379> SDIFF myset1 myset2 #setThe difference set between, it could be more than oneset
1) "1"
2) "2"
127.0. 01.:6379> SINTER myset1 myset2 #setThe intersection between, can be multipleset
1) "3"
2) "4"
3) "5"
127.0. 01.:6379> sunion myset1 myset2 #setThe union between, which can be multipleset
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "Seven"
Copy the code

⑤ Conclusion: It can realize the needs of mutual friends and common attention.

  1. Hash.

(1) Hset (adding hash), hGET (querying), hGEtall (querying all), hdel (deleting the hash value), hlen (obtaining the hash length), and hEXISTS (determining whether the key exists) operations

127.0. 01.:6379> hset myhash name dingdada age 23# Add hash (integer)2
127.0. 01.:6379> hget myhash name # Get the hash value where key is name"dingdada"
127.0. 01.:6379> hget myhash age # Obtain the hash value where key is age"23"
127.0. 01.:6379> hgetall myHash # Get all values in the hash, including key1) "name"
2) "dingdada"
3) "age"
4) "23"
127.0. 01.:6379> hset myhash del test1
127.0. 01.:6379> hgetall myhash
1) "name"
2) "dingdada"
3) "age"
4) "23"
5) "del"
6) "test"
127.0. 01.:6379> hdel myhash del age # Hdel myhash del age # Hdel myhash2
127.0. 01.:6379> hgetall myhash
1) "name"
2) "dingdada"
127.0. 01.:6379> hlen myhash # Obtain the length of the hash, equal to length, size (integer)1
127.0. 01.:6379> HEXISTS myHash Name # Specifies whether the key exists in the hash1
(integer) 1
127.0. 01.:6379> HEXISTS myHash Age # Specifies whether the key exists in the hash0
(integer) 0
Copy the code

(2) hkeys (obtains all keys), hVALS (obtains all values), hincrby (increments values), and hsetNx (does not exist) operations

127.0. 01.:6379> hset myhash age 23 high 173
(integer) 2
127.0. 01.:6379> hgetall myhash
1) "name"
2) "dingdada"
3) "age"
4) "23"
5) "high"
6) "173"
127.0. 01.:6379> hkeys myhash # Get all keys in the specified hash1) "name"
2) "age"
3) "high"
127.0. 01.:6379> hvals myhash # Retrieves all values in the specified hash1) "dingdada"
2) "23"
3) "173"
127.0. 01.:6379> hincrby myhash age 2# Make the value of age in the hash specify +2(increment) (integer)25
127.0. 01.:6379> hincrby myhash age - 1Let the value of age in the hash be specified- 1(decrement) (integer)24
127.0. 01.:6379> hsetNx myhash nokey novalue1 
127.0. 01.:6379> hsetNx myhash name miaotiao #0
(integer) 0
127.0. 01.:6379> hgetall myhash
1) "name"
2) "dingdada"
3) "age"
4) "24"
5) "high"
6) "173"
7) "nokey"
8) "novalue"
Copy the code

③ Conclusion: It is more suitable for storing objects than String

  1. ZSet (ordered set)

Zadd (add), zrange (query), zrangebyscore (sort small – big), zrevrange (sort large – small), zrangebyscore withscores (query all values contain key

127.0. 01.:6379> zadd myzset 1 one 2 two 3Three # Add zset value (integer)3
127.0. 01.:6379> ZRANGE myzset 0 - 1# Query all values1) "one"
2) "two"
3) "three"#-inf minus infinity + INF plus infinity127.0. 01.:6379> ZRANGEBYSCORE myzset -inf + INF #1) "one"
2) "two"
3) "three"
127.0. 01.:6379> ZRANGEBYSCORE myzset 0 1# Query only key<=1And sort from small to large1) "one"
127.0. 01.:6379> ZREVRANGE myzset 1 - 1Sort output from large to small1) "two"
2) "one"
127.0. 01.:6379> ZRANGEBYSCORE myzset -inf +inf withscores1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
Copy the code

② Zrem (remove elements), zcard (view the number of elements), and zCount (query the number of elements in the specified range) operations

127.0. 01.:6379> zadd myset 1 v1 2 v2 3 v3 4 v4
(integer) 4
127.0. 01.:6379> ZRANGE myset 0 - 1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0. 01.:6379> zrem myset v3 # remove (integer)1
127.0. 01.:6379> ZRANGE myset 0 - 1
1) "v1"
2) "v2"
3) "v4"
127.0. 01.:6379> zcard myset #查看zset myset (integer)3
127.0. 01.:6379> zcount myset 0 100# Query the number of elements within the specified range (integer)3
127.0. 01.:6379> zcount myset 0 2# Query the number of elements within the specified range (integer)2
Copy the code

③ Summary: table sorting, salary table sorting, age sorting and other needs can be used to achieve ZSET!


I see no ending, but I will search high and low

So much for the five types of Redis, if you think I blogger wrote a good job! Writing is not easy, please like, follow, comment to give the blogger a encouragement ~hahaa