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> multi
# The entered command is put into the transaction queue, and the returned queued indicates that it has been put into the transaction queue> sadd user:1:following 2
QUEUED> sadd user:2:followers 1
# 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> 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> multi
OK> set name jack
QUEUED> set age
(error) ERR wrong number of arguments for 'set' command> 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> exec
(error) EXECABORT Transaction discarded because of previous errors.

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

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.> set aaaa 1
OK> watch aaaa
OK> set aaaa 2
OK> multi
OK> set aaaa 3
QUEUED> exec
(nil)> get aaaa
"2"> set aaaa 4
OK> get aaaa

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> set expirekey djkfj
# 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> expire expirekey 900
(integer) 1
# See how much time is left to expire> ttl expirekey
(integer) 785
# Returns - 1 if the key expires permanently> ttl aaaa
(integer) -1> expire aaaa 2
(integer) 1
# If the key expires and the key is deleted, the ttl command returns - 2> ttl aaaa
(integer) -2> set aaaa 11
OK> expire aaaa 300
(integer) 1
# Expiration time of clear key> persist aaaa
(integer) 1> ttl aaaa
(integer) -1
# The expiration time is also cleared after the value is reset> expire aaaa 20
(integer) 1> set aaaa 4
OK> ttl aaaa
(integer) -1
# After the time is reset, the expiration time is reset> expire aaaa 20
(integer) 1> ttl aaaa
(integer) 12> expire aaaa 20
(integer) 1> 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> sadd setDD 1 3 6 2 5 9
(integer) 6> sort setDD
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
6) "9"> lpush mtlisht 2 4 1 3 7
(integer) 5> sort mtlisht
1) "1"
2) "2"
3) "3"
4) "4"
5) "7"
# Ordered set sorting ignores fractions and sorts by value> zadd myzet 30 1 40 4 10 5 20 2
(integer) 4> 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> 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> sort listA
(error) ERR One or more scores can't be converted into double> 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> sort listA alpha
1) "a"
2) "b"
3) "c"
4) "d"
5) "f"
6) "r"> 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> sort listA alpha
1) "a"
2) "b"
3) "c"
4) "d"
5) "f"
6) "r"> 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