服務器經(jīng)常會用到redis作為緩存,有很多數(shù)據(jù)都是臨時set以下,可能用過之后很久都不會再用到了(比如暫存session)那么就有幾個問題了
光陰似箭催人老,日月如移越少年。
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:
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.