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

How to expire the Value of Redis

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to make the Value of Redis expire. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Redis's persistence feature is one of its highlights, but it will not be used by us for persistent storage. Therefore, how to make the key or value of Redis expire is very important! Today, I will simply say, this interview question!

Redis itself has its own expiration mechanism, but this expiration mechanism is only for specific Key expiration, and there is no specific function for how Value expires. However, in some scenarios, it is often necessary to let the Value of Redis expire, while the Key does not expire.

For example, we now have a need to do a reading count of articles within 15 minutes. And require statistics every minute, find out hot articles, do special treatment. Each article is marked with a unique ID.

For example, the changes in the number of articles read are as follows:

[100, 520, 666, 888, 996...]

If there are too many articles, it will waste a lot of memory space! We need to clean up some Value data in time, and let the Key expire, which may not be suitable for our scenario!

So how do you do it?

At this time, the ordered collection of Redis can come in handy!

You can specify score for the specified value, and then sort it by score, and then use ZRANGEBYSCORE to get the value corresponding to score in a certain range.

ZRANGEBYSCORE key score1 # articles id timestamp readings ZADD id 1577591750 996

Now, if we want to get data within 15 minutes, we can do this:

ZRANGEBYSCORE id now-15 * 60 now

But it is still not perfect, because although our value is correct, the Value has not expired.

Therefore, in view of the above command, we can also optimize it!

ZRANGEBYSCORE id 0 now-15 * 60

Finally, you need to note that in addition to Value expiration, key should also note that key also needs to be set to expire.

# whole key expires after 24 hours EXPIRE key 24 * 60 * 60

My example above may not be particularly appropriate, and it is also aimed at ordered sets. If it is a non-ordered collection, some netizens replied that the get value is first, and then the old data is eliminated, and then the set value is re-established. However, my advice is that choosing the right data structure is more important!

On how to make Redis Value expired to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Servers

Wechat

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

12
Report