Search code examples
redislru

Is it possible to define LRU Redis that will apply only of part of the keys


I currently i used the same redis instance to save 2 types of informations:

  1. That contain cache data that can be evicted when i want to free memory (for this data i want to use an LRU , as it's can grow to very large number of records).
  2. Information that can't be deleted randomly - and should be cleanup by explicit delete command from the app.

My question is if there is a way to separate between those 2 kinds of data in a manner that enable to define LRU only on the data of type 1 , but not on the data of type 2.


Solution

  • For the first type of data, you can set a timeout i.e. TTL, for each key (with EXPIRE, EXPIREAT, or SET command), and for the second type of data, set no timeout. Then you can config your Redis server to set maxmemory-policy as volatile-lru.

    With this setting, only the first type of data will be evicted with LRU algorithm.