Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to expire the Value of Redis

Shulou Source: shulou.com Published: 2022-05-31 17:33:50 09月17日 Update

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.

Tags: Articles data ordered important content scenarios more mechanisms articles help statistics good appropriate appropriate special right not enough highlights examples interests Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft MySQL Shulou Technology Redmi