In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the Java operation redis setting the solution that expires in the early morning of the next day. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Java operation redis sets the scene that expires in the early hours of the next day.
When querying data, we encountered the problem of setting the data to expire the next day in redis, but redis did not have a corresponding API, so we had to solve it on our own.
Train of thought
Calculate the time difference between the next morning and the current time, and set the time difference to the expiration time of redis, we can achieve the desired effect.
The code / * calculates the number of seconds between the next morning and the current time * @ param * @ return java.lang.Long * @ author shy * @ date 18:10 * / public static Long getNowToNextDaySeconds () {Calendar cal = Calendar.getInstance (); cal.add (Calendar.DAY_OF_YEAR, 1); cal.set (Calendar.HOUR_OF_DAY, 0) Cal.set (Calendar.SECOND, 0); cal.set (Calendar.MINUTE, 0); cal.set (Calendar.MILLISECOND, 0); return (cal.getTimeInMillis ()-System.currentTimeMillis ()) / 1000;}
Once you get the time difference, there is basically no problem with the rest.
Attached is the Redis tool class:
/ * * Operation redis * @ author shy * @ date 10:01 on 2020-12-10 * / @ Servicepublic class RedisService {@ Autowired private StringRedisTemplate stringRedisTemplate; @ Autowired private RedisTemplate redisTemplate / * determine whether String type key exists * * @ param key * @ return * @ author shy * @ date November 13, 2018 1:40:37 * / public boolean hasStringKey (String key) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException () } return stringRedisTemplate.opsForValue () .getOperations () .hasKey (key) } / * determine whether String type key exists * * @ param key * @ return * @ author shy * @ date November 13, 2018 1:43:51 * / public boolean nonStringKey (String key) {return! hasStringKey (key) } / * set String type key,String type value, expiration time timeout TimeUnit * * @ param key * @ param value * @ param timeout * @ param timeUnit * @ author shy * @ date 10 December 2018 13:53:38 * / public void setStringKey (String key, String value, Long timeout TimeUnit timeUnit) {if (StringUtils.isBlank (key) | | Objects.isNull (timeout)) {throw new EmptyParameterException () } stringRedisTemplate.opsForValue () .set (key, value, timeout, timeUnit);} public void setStringKey (String key, String value) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException ();} stringRedisTemplate.opsForValue () .set (key, value) } / * get String type value * * @ param key * @ return * @ author shy * @ date 7:09:31 * / public String getStringValue (String key) {if (StringUtils.isBlank (key)) { Throw new EmptyParameterException () } return stringRedisTemplate.opsForValue () .get (key) } / * obtain the expiration time of Key * * @ param key * @ return * @ author shy * @ date 17:28:36 on April 25, 2019 * / public Long getExpire (String key) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException () } return stringRedisTemplate.getExpire (key) } / * set the expiration time of Key * * @ param key * @ return * @ author shy * @ date April 25, 2019 17:28:36 * / public Boolean setExpire (String key,Long timeout TimeUnit timeUnit) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException () } return stringRedisTemplate.expire (key, timeout, timeUnit) } / * value + n * @ param key * @ return * @ author shy * @ date 15:54:30 on April 8, 2019 * / public Long setIncrementValue (String key) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException () } return stringRedisTemplate.opsForValue () .increment (key, 1L) } / * set String type key,Object type value Expiration date timeout * * @ param key * @ param value * @ param timeout * @ author shy * @ date 10 December 2018 13:54:07 * / public void setObjectKey (String key, Object value, Long timeout TimeUnit time) {if (StringUtils.isBlank (key) | | Objects.isNull (timeout)) {throw new EmptyParameterException () } redisTemplate.opsForValue () .set (key, value, timeout, time);} public void setObjectKey (String key, Object value) {if (StringUtils.isBlank (key)) {throw new EmptyParameterException ();} redisTemplate.opsForValue () .set (key, value) } / * get Object type value * * @ param key * @ param clazz * @ author shy * @ date 10:01:30 * / @ SuppressWarnings ("unchecked") public T getObjectValue (String key) Class clazz) {if (StringUtils.isBlank (key)) {return null } return (T) redisTemplate.opsForValue () get (key) } / * remove a single String type key * * @ param key * @ author shy * @ date November 13, 2018 10:42:01 * / public void removeSingleStringKey (String key) {if (StringUtils.isBlank (key)) { Throw new EmptyParameterException () } stringRedisTemplate.opsForValue () .getOperations () .delete (key) } / * remove Collection type keys * * @ param keys * @ author shy * @ date 13 November 2018 3:15:16 * / public void removeMultiStringKey (Collection keys) {if (CollectionUtils.isNotEmpty (keys)) { StringRedisTemplate.opsForValue (). GetOperations (). Delete (keys) }} / * redis key Fuzzy query * @ author shy * @ date 11:21:45 on January 4, 2021 * @ param key * @ return * / public Set queryStringKeys (String key) {return redisTemplate.keys (key + "*") Introduction to the function of redis expiration policy
When we use redis, we usually set an expiration time, of course, some do not set an expiration time, that is, it will not expire forever.
When we set the expiration time, how does redis determine whether it expires and according to what strategy to delete it.
Set expiration time
When we set key, we can give an expire time, that is, the expiration time, and specify that this key can only survive for an hour. Suppose you set a batch of key to survive for one hour, then how does redis delete this batch of key after the next hour?
The answer is: regular deletion + lazy deletion.
The so-called periodic deletion means that redis randomly selects some key with expiration time every 100ms by default to check whether they have expired or not, and delete them if they expire. Note that it is not every 100ms that traverses all the key that sets the expiration time. That would be a performance disaster. In fact, redis is a random sample of key every other 100ms to check and delete.
But the problem is that regular deletion may cause a lot of expired key not to be deleted by time, so delete it lazily, that is, when you get a key, redis will check whether the key has expired if the expiration time is set. It will be deleted if it expires.
Through the combination of the above two methods, it is guaranteed that expired key will be killed. Therefore, it is not that all expired key will be deleted when it comes to the expiration time, which is why the memory footprint will not be reduced when it expires.
But in fact, this is still a problem. If you delete a lot of expired key regularly, and then don't delete it in time, it will cause a large number of expired key to accumulate in memory and consume redis memory. How to deal with this situation?
The answer is: follow the memory elimination mechanism.
Memory obsolescence
If redis takes up too much memory, some elimination will take place at this time, and there are some strategies as follows:
Noeviction: when there is not enough memory to hold the newly written data, the newly written data will report an error, which is generally not used in this actual scenario.
Allkeys-lru: when there is not enough memory to hold newly written data, remove the least used key in the key space (this is the most commonly used)
Allkeys-random: when there is not enough memory to hold newly written data, a key is randomly removed from the key space, which is less commonly used.
Volatile-lru: when there is not enough memory to hold newly written data, remove the least recently used key from the key space where the expiration time is set.
Volatile-random: when there is not enough memory to hold newly written data, a key is randomly removed from the key space where the expiration time is set.
Volatile-ttl: when there is not enough memory to hold newly written data, key with an earlier expiration time is removed first in the key space where the expiration time is set.
Memory obsolescence triggers the obsolescence condition to delete some key, which is the reason why the key is lost without setting the expiration time.
This is the solution that Xiaobian shared the Java operation redis to expire in the wee hours of the next day. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.