redis Affairs and expiration date

1. Transaction

redis in transaction is a set of commands. redis transaction: multi-> queued-> exec.

redis ensure that all the commands in a transaction are either executed or not executed. If the exec command before sending the client disconnected, redis will clear the transaction queue, all the commands in the transaction will not be executed. Once the client sends the exec command, all of the command will be executed, even if the client disconnected thereafter does not matter, because redis command has been recorded for all to be executed.

If a transaction is a command execution error:

1) syntax error: As long as there is a syntax error in command, after executing exec command redis will directly return an error, even grammatically correct command is not executed.

2) Run Error: redis transaction does not roll back the transaction provided by the relational database, the developer must clean up their stall to this end in the rest of the transaction execution after an error (database recovery back to the state before the transaction execution, etc.).

Watch : Watch command can monitor one or more keys, once one of which keys were modified or deleted after the transaction will not be executed.

Monitoring of all key cancels after an exec command, if you do not want to execute a transaction commands can also be used unwatch command to cancel the monitoring.

2. Expiration Time

seconds The Key The expire , in seconds. 1 represents a setting command returns expire successful, returns 0 represents a bond or absence of failure is provided. pexpire key millisecond, milliseconds.

Key TTL : Returns the key remaining time (in seconds). -2 returned key does not exist If key is not set expiration -1.

Key the persist : Cancel key expiration time set.

In order to increase the load capacity of the site, often need access to some of the higher frequencies but cpu io resource consumption or greater operating results cached, and want to make these cache automatically expire after a period of time.

When the server memory is limited, if a large number of keys and use of caching expiration time too long will cause fill redis memory; if on the other hand in order to prevent excessive redis memory for cache key and the expiration time is set too short, it could lead to the cache hit rate is too low and a lot of memory is idle. The actual development in fact very difficult to set a reasonable bond for the cache expiration time, for which you can limit the maximum memory redis can use, and let redis according to certain rules eliminate unnecessary cache key, this approach is very common.

maxmemory bytes

maxmemory-policy xxx: When out of maxmemory redis will be based maxmemory-policy parameter specifies the policy to remove unwanted key until redis less than the specified amount of memory RAM.

Rules redis support phase-out of key Explanation
volatile-lru Lru delete algorithm using a key (only key to set an expiration time)
allkeys-lru To delete a key algorithm using lru
volatile-random To delete a random key (only key to set an expiration time)
allkeys-random To delete a random key
volatile-ttl Delete recent expiration of a key
noeviction Do not delete key, only returns an error

Guess you like

Origin www.cnblogs.com/i-hard-working/p/12037932.html