Redis如何回收过期数据?
大家讲道理
大家讲道理 2017-04-21 11:15:38
0
2
718

服务器经常会用到redis作为缓存,有很多数据都是临时set以下,可能用过之后很久都不会再用到了(比如暂存session)那么就有几个问题了

  1. redis会自己回收清理不用的数据吗?
  2. 如果能,如何配置?
  3. 如果不能,如何防止数据累加后大量占用存储空间的问题?
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
小葫芦

redis stores all data permanently in memory by default. When you need to persist data (save to disk) or automatically delete expired keys, you need to use additional commands or configurations to complete; If you need to use redis to complete your business requirements (such as cache), then you must follow the design rules of redis;

As for the question you mentioned later: Even if I add expire to every one of my mysophobic items, can you guarantee that other people in multiplayer development also add expire? If someone doesn’t have it, then this data will always be saved

This is what I think: Usually business logic is encapsulated into an API interface. For example, the session interface for login scenarios may be: void addLoginSession(string data, int timeout). Then when others call it, they don’t even care whether redis is used. Or memcache, all details must be digested only by API designers;

By the way, how does redis clean up expired keys in the database? It is divided into two types:

  1. Lazy deletion: When you operate a key (such as get name), redis will first check whether the key is associated with a timeout. If so, check whether it has timed out. If it times out, it will return null, otherwise it will return the corresponding value;
  2. Scheduled deletion: There is a time event in redis, which will clean up expired keys in the database (redis will limit the time occupied by this operation to avoid blocking client requests)
Peter_Zhu

You can use the expire command to set the expiration time. There are several other commands that can also be used to set the expiration time. Please refer to the documentation for details.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template