Get the App
SLTechnology News&Howtos  ›  Database  › 

Setting, getting and deleting expiration time of Redis expiration key

Shulou Source: shulou.com Published: 2022-06-01 13:05:32 10月02日 Update

Setting, getting and deleting expiration time of Redis expiration key

Set expiration

By default, keys have no lifetime, that is, they never expire unless the memory is cleared.

The lifetime of the set key is also easy, and can be set by four commands (either): EXPIRE, PEXPIRE, EXPIREAT, and PEXPIREAT commands, which look different but are all implemented in the same way. The commonly used ones are EXPIRE and PEXPIRE, the former setting the expiration time in seconds and the latter in milliseconds.

The command states that EXPIRE key # s sets the survival time of KEY to # seconds PEXPIRE key # ms sets the survival time of KEY to # milliseconds EXPIREAT key timestamp sets the survival time of KEY to UNIX timestamp, in seconds PEXPIREAT key timestamp sets the lifetime of KEY to UNIX timestamp, in milliseconds

The above four commands are only different in units and representations, but in fact, the execution of EXPIRE, PEXPIRE, and EXPIREAT commands will end up using PEXPIREAT.

For example, if you use EXPIRE to set the lifetime of KEY to N seconds, how does the background run:

It calls the PEXPIRE command to convert N seconds to M milliseconds.

Then get the current UNIX time unit in milliseconds

Pass the current UNIX time plus M milliseconds to PEXPREAT

Set the expiration time for the key, which is saved in a dictionary, which is also the key value structure, the key is a pointer to the real key, and the value is a long integer UNIX time.

Set in seconds, set the aaa key to expire after 1 second, and get the aaa key after 10 seconds will return null.

192.168.20.66 set aaa 7000 > set aaa 123-> Redirected to slot [10439] located at 192.168.20.62:7000OK192.168.20.62:7000 > get aaa "123" 192.168.20.62 get aaa 7000 > expire aaa 10 (integer) 1192.168.20.62 2V 7000 > ttl aaa (integer) 7192.168.20.62 7000 > ttl aaa (integer) 3192.168.20.62 7000 > ttl aaa (integer)-2192.168.20.62 7000 > get aaa (nil)

Set in milliseconds, set the key bbb to expire after 10000 milliseconds.

192.168.20.62pexpire bbb 7000 > set bbb 898-> Redirected to slot [5287] located at 192.168.20.65:7000OK192.168.20.65:7000 > pexpire bbb 10000 (integer) 1192.168.20.65V 7000 > ttl bbb (integer) 6192.168.20.65V 7000 > ttl bbb (integer) 2192.168.20.65V 7000 > ttl bbb (integer)-2192.168.20.65V 7000 > get bbb (nil) get key expiration time

View the lifetime of a key (in seconds):

Ttl key

Another command is pttl key, which is displayed in milliseconds.

Description of the return value of the expiration time

| | seconds value | description |

| |-2 | expired and deleted |

| |-1 | No expiration time setting, that is, never expire |

| | > 0 | indicates how many seconds or milliseconds are left before expiration | |

Expiration time of the clear key

You can use the PERSIST command to remove the expiration time of a key:

192.168.20.65 expire aaa 7000 > set aaa hello- > Redirected to slot [10439] located at 192.168.20.62:7000OK192.168.20.62:7000 > expire aaa 100 (integer) 1192.168.20.62Vl7000 > ttl aaa (integer) 97192.168.20.62Vl7000 > persist aaa (integer) 1192.168.20.62Vl7000 > ttl aaa (integer)-1192.168.20.627000 > get aaa "hello"

Tags: Time command unit different never two that is memory principle but can pass background dictionary actual actually that is commonly used form situation point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi OPPO Reno NVidia MySQL