Home > Database > Redis > body text

How to solve the problem of redis expiration time

王林
Release: 2023-05-27 16:49:19
forward
2013 people have browsed it

1. If you modify a redis' String expiration key multiple times, how to ensure that it can still retain the deletion time when it was first set

For modifying String, redis has: set, setex, append , incr, decr, etc. Among them, using set and setex to modify the original existing String will reset the originally set expiration time. Strictly speaking, set and setex are not "modifications" but overwriting, so the original settings If the key-value has been passed, set/setex this key again will overwrite the original one. The test results using try redis are as follows:

How to solve the problem of redis expiration time

If you use append, incr, decr and other commands to modify, the original expiration time will not be reset:

How to solve the problem of redis expiration time

So, for the string structure, as long as the set /setex command is executed, the expiration time will be reset, and the set command will directly change the expiration time to never expire.
For example, if I set a {key1:value} at 7:00, the expiration time is 30 minutes, and if I set/setex the value of this key multiple times between 7:00-7:30, the expiration time of key1 will be reset. settings and will eventually not expire at 7:30. How to ensure that the expiration time is not refreshed every time after setting this expiration key, and the expiration is stable at 7:30?

Method 1:

Use Timer, create a timer when setting for the first time, and delete the key when it expires. This method will occupy more CPU resources when the amount of data is large, and is not recommended.

Method 2:

Before setting the value of the key each time, return the remaining expiration time of the key at this time, and assign the remaining expiration time to the key as the new expiration time during setex Time will solve it.

Taking the Spring project as an example, it can be solved by using the remaining expiration timestamp (millisecond level) returned by the redisTemplate.getExpire(String key) method. Essentially, it sends a PTTL command to redis to return milliseconds. The remaining expiration time of the unit's key.

How to solve the problem of redis expiration time

Code example:

Let key1 keep the expiration time when it is first set every time it is modified

//获取key1的剩余时间的时间戳
Long expire = redisTemplate.getExpire("key1");
//最后一个参数可以选秒、毫秒(TimeUnit.MILLISECONDS),Redis最多只能返回毫秒级别的key的剩余过期时间
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
Copy after login

2 .Will modifying the values ​​of hash, set, Zset, and list reset the expiration time?

The validity period of K-V of String type will be re-timed as the value value is modified:

If there is a K-V, the expiration time is 30 seconds. If the value value is modified, Then the expiration time will be reset to 30 seconds, instead of the original set time minus the lost time.

Except for the string data structure, modifications to other data structures will not reset the expiration time

For example, hash, set, Zset, list, etc.:

Take hash as an example:

The validity period of hash will not be re-timed as the hash field value is modified.
If you need to retime, you need to re-specify the validity period when modifying the field value.

How to solve the problem of redis expiration time

The above is the detailed content of How to solve the problem of redis expiration time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!