Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Setting, getting and deleting expiration time of Redis expiration key

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report