Redis transactions and expiration times are described in detail

  • 2020-05-24 06:27:20
  • OfStack

Redis transactions and expiration times are described in detail

1. Redis transactions:

Transactions are supported in Redis, which means that when we need to execute several commands, either none of them will be executed or all of them will be executed:

1. Start transaction writing:


  multi

2. Then write the command, note that after each command to be executed, press enter, and the command will automatically join the team:


  lpush  art:1  hello 
  lpush  art:1  nihao

3. Executive affairs:


  exec

Redis guarantees that all commands in the transaction are either executed or not.

2. Expiration time of Redis:

In actual development, some time-sensitive data is often encountered, such as cache, which needs to be deleted after a period of time. Redis supports setting the expiration time of a key, and after the key expires, its corresponding value will be 1 and cleared.

1. Newly set 1 key value:


set art:name hello

2. Set its cache time (note that it is in seconds)


expire art:name 20

3. After 20 seconds, the value is null


get art:name
(nil)

4. How long will it take for a value to be deleted?


ttl art:name

It returns in seconds

5. Set its cache time (in milliseconds)


pexpire art:name 20000 

6. Set the expiration time from 1970 to the present:


expireat art:name 1351858600 ( For the second time )
pexpireat art:name 1351858600 ( The time is milliseconds )

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: