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:
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;
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)
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.
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.