0

I have following cache structure where cache key is composite key usecase_actualKey. Is it possible to write a custom eviction policy that keep only x entries for useCaseA_* and y entries of useCaseB_*

myTestCache =  {

useCaseA_someKey1 : dummyValue1
useCaseA_someKey2 : dummyValue2
useCaseB_someOtherKey1 : dummyValue1_somestuff
useCaseB_someOtherKey2 : dummyValue1_somestuff2

}

Consider max size of myTestCache is 300

I have 100 entries for useCaseA and 200 for useCaseB

when I make new entry [101 entry] with useCaseA_ I want to evict least frequently element from useCaseA [we have 100 entries for usecaseA]

This can be easily achieved by mapOfCache like Map<String, CaffeineCache<String, String>>

I checked this thread How many Caffeine Cache instances in an application is too much? . Seems this approach may have GC load.

So I am trying to see if this can be achieved via flat structure

Jeya Kumar
  • 1,002
  • 1
  • 13
  • 36
  • 1
    The eviction policy is not customizable and is intended to maximize the hit rate. Typically a business rule like yours doesn't help in that regard, but might be useful for problems like per-tenet cache restrictions. Instead of making the cache somehow understand and optimize for the domain, it's more straightforward to have different cache instances servicing those cases explicitly. The cost of having a new cache instance is tiny (a bit more than another map instance). – Ben Manes Apr 04 '23 at 19:17

0 Answers0