Redis advanced: transaction, expiration time, sorting

1. affairs

1.1 overview
A transaction in Redis is a collection of commands. All the commands in a transaction are executed or none of them are executed. The principle of transaction is to first send the commands belonging to a transaction to Redis, and then let Redis execute these commands in turn

# Tell redis not to execute the commands entered later, and save them temporarily
127.0.0.1:6379> multi
OK
# The entered command is put into the transaction queue, and the returned queued indicates that it has been put into the transaction queue
127.0.0.1:6379> sadd user:1:following 2
QUEUED
127.0.0.1:6379> sadd user:2:followers 1
QUEUED
# Tell redis to execute all the commands in the transaction queue waiting to be executed in the order of sending. The return value is the return value of these commands
127.0.0.1:6379> exec
1) (integer) 1
2) (integer) 1

Redis ensures that all the commands in a transaction are executed or not executed. If the client is disconnected before sending the exec command, redis will clear the transaction queue. Redis's transaction also ensures that the commands in a transaction are executed in turn without being inserted by other commands.

1.2 error handling
(1) Syntax error: the command does not exist or the number of command parameters is wrong

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name jack
QUEUED
127.0.0.1:6379> set age
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379> ersf age
(error) ERR unknown command 'ersf'
# As long as there is a command syntax error, executing exec will directly return an error, and the correct syntax will not execute either
127.0.0.1:6379> exec
(error) EXECABORT Transaction discarded because of previous errors.

(2) Run error: error during command execution

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set ass 1
QUEUED
# Command of hash type is used to operate key of collection type. No error can be found before execution
127.0.0.1:6379> sadd ass 2
QUEUED
127.0.0.1:6379> set ass 3
QUEUED
# If a command in a transaction runs incorrectly, other commands will still execute
127.0.0.1:6379> exec
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
3) OK
127.0.0.1:6379> get ass
"3"

1.3 introduction to watch command
The watch command can monitor one or more keys. Once one of the keys is modified (or deleted), subsequent transactions will not be executed. The monitoring continues until the exec command is executed.

127.0.0.1:6379> set aaaa 1
OK
127.0.0.1:6379> watch aaaa
OK
127.0.0.1:6379> set aaaa 2
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set aaaa 3
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get aaaa
"2"
127.0.0.1:6379> set aaaa 4
OK
127.0.0.1:6379> get aaaa
"4"

The exec command will cancel the monitoring of all keys. We can also use the unwatch command to cancel the monitoring.

2. Expiration time

# Set expiration time for key
expire key seconds 

127.0.0.1:6379> set expirekey djkfj
OK
# Set the expiration time of 15 minutes, return 0 to indicate that the key does not exist or the setting fails, 1 to indicate success
127.0.0.1:6379> expire expirekey 900
(integer) 1
# See how much time is left to expire
127.0.0.1:6379> ttl expirekey
(integer) 785
# Returns - 1 if the key expires permanently
127.0.0.1:6379> ttl aaaa
(integer) -1
127.0.0.1:6379> expire aaaa 2
(integer) 1
# If the key expires and the key is deleted, the ttl command returns - 2
127.0.0.1:6379> ttl aaaa
(integer) -2

127.0.0.1:6379> set aaaa 11
OK
127.0.0.1:6379> expire aaaa 300
(integer) 1
# Expiration time of clear key
127.0.0.1:6379> persist aaaa
(integer) 1
127.0.0.1:6379> ttl aaaa
(integer) -1
# The expiration time is also cleared after the value is reset
127.0.0.1:6379> expire aaaa 20
(integer) 1
127.0.0.1:6379> set aaaa 4
OK
127.0.0.1:6379> ttl aaaa
(integer) -1
# After the time is reset, the expiration time is reset
127.0.0.1:6379> expire aaaa 20
(integer) 1
127.0.0.1:6379> ttl aaaa
(integer) 12
127.0.0.1:6379> expire aaaa 20
(integer) 1
127.0.0.1:6379> ttl aaaa
(integer) 16

The seconds parameter of the expire command must be an integer, and the minimum unit is 1 second. If you want a more accurate expiration time, you can use pexpire, the time unit is milliseconds, and pttl can view the remaining expiration time.
Note: if the watch command is used to monitor a key with an expiration time, the key will be automatically deleted when the key expires and will not be deemed to be changed by the watch command

Tips: when using redis as a cache server, sometimes it is difficult to set a reasonable expiration time for a key. In fact, we can let redis eliminate unnecessary cache keys according to certain rules. This method is very useful when only redis is used as a cache system.
Specific setting method: modify the maxmemory parameter of the configuration file to limit the maximum available memory size of redis (in bytes). When the limit is exceeded, redis will delete the unnecessary keys according to the policy specified by maxmemory policy parameter until the memory occupied by redis is less than the specified memory
Allkeys LRU rule: if the specified memory is exceeded, the most recently used key in the database will be deleted. See P78 for other rules

3. ranking

3.1 sort command

127.0.0.1:6379> sadd setDD 1 3 6 2 5 9
(integer) 6
127.0.0.1:6379> sort setDD
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
6) "9"
127.0.0.1:6379> lpush mtlisht 2 4 1 3 7
(integer) 5
127.0.0.1:6379> sort mtlisht
1) "1"
2) "2"
3) "3"
4) "4"
5) "7"
# Ordered set sorting ignores fractions and sorts by value
127.0.0.1:6379> zadd myzet 30 1 40 4 10 5 20 2
(integer) 4
127.0.0.1:6379> sort myzet
1) "1"
2) "2"
3) "4"
4) "5"

In addition to arranging numbers, you can also arrange non numeric elements according to the dictionary

127.0.0.1:6379> lpush listA a c r d f b
(integer) 6
# If alpha is not added, the sort command attempts to convert all elements to double precision floating-point numbers for comparison. If it fails to convert, an error will be prompted
127.0.0.1:6379> sort listA
(error) ERR One or more scores can't be converted into double
127.0.0.1:6379> sort listA alpha
1) "a"
2) "b"
3) "c"
4) "d"
5) "f"
6) "r"

sort command is arranged in order from small to large by default, or in reverse order. desc parameter

127.0.0.1:6379> sort listA alpha
1) "a"
2) "b"
3) "c"
4) "d"
5) "f"
6) "r"
127.0.0.1:6379> sort listA alpha desc
1) "r"
2) "f"
3) "d"
4) "c"
5) "b"
6) "a"

The sort command also supports paging. The limit offset count parameter means to skip the previous offset elements and get the later count elements

127.0.0.1:6379> sort listA alpha
1) "a"
2) "b"
3) "c"
4) "d"
5) "f"
6) "r"
127.0.0.1:6379> sort listA alpha limit 1 2
1) "b"
2) "c"
86 original articles published, 14 praised, 10000 visitors+
Private letter follow

Tags: Redis less Database

Posted on Mon, 10 Feb 2020 03:22:57 -0500 by exploo