Deven
Deven

Reputation: 617

Redis: hashmap with size limit and LRU eviction functionality

Lets say I have some keys in a redis store. I want to keep some key value pairs in a new hashmap structure. I also want to keep a limit on the size of this hashmap and evict the least recently used key value pair of the hashmap when its size (hashmap) grows beyond a limit and not touch the rest of the already present redis data structures. Does redis provide me with any such functionality where I can do this LRU style eviction of hashmap entries not touching rest of the stored keys? Or can one build it on top of what redis provides in any way? Thanks for the help!

Upvotes: 2

Views: 2202

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50122

Does redis provide me with any such functionality where I can do this LRU style eviction of hashmap entries not touching rest of the stored keys?

No, it doesn't.

Or can one build it on top of what redis provides in any way?

Yes, one can.

There are 3 ways one could go about it:

  1. Client-side logic: you can manage the Hash's fields eviction logic in your application. This will require storing additional (meta) data in the Hash's values (i.e. delimit/structure the meta and real data in the value), at the Hash's level (you can use "special" field names, like "_eviction_heap_"), and/or with additional data structures (looks like a Sorted Set per Hash would be useful).

  2. Server-side Lua: for optimizing the above, you can package the logic in Lua and execute it with the EVAL command.

  3. Redis modules: this is the advanced stuff, but if you're up to it you can pretty much do anything - including implementing a new "hashmap with size limit and LRU eviction functionality" data structure.

Upvotes: 4

Related Questions