Any Java caches that can limit memory usage of in-memory cache, not just instance count?

后端 未结 8 2485
后悔当初
后悔当初 2021-02-14 00:08

I am looking for a simple in-memory (and in-process) cache for short-term caching of query data (but short-term meaning beyond request/response, i.e. session boundary). EhCache

相关标签:
8条回答
  • 2021-02-14 00:54

    I agree with Paul that this is often solved by using a soft reference cache, though it may evict entries earlier than you prefer. A usually acceptable solution is to use a normal cache that evicts to the soft cache, and recovers entries on a miss if possible. This victim caching approach works pretty well, giving you a lower bar but extra benefit if free memory is available.

    The memory size can be determined by enabling the Java agent, and usage is pretty simple when using the SizeOf utility (http://sourceforge.net/projects/sizeof). I've only used this for debugging purposes, and I'd recommend benchmarking the overhead before adopting it for normal usage.

    In my caching library, I am planning on adding the ability to plug in a evaluator once the core algorithm is implemented. This way you could store a collection as the value, but bound the cache by the sum of all collection sizes. I have seen unbounded collections as values in caches cause OutOfMemoryExceptions, so having control is quite handy.

    If you really need this, and I'd advise not to, we could enhance my current implementation to support this. You can email me, ben.manes-at-gmail.com.

    0 讨论(0)
  • 2021-02-14 01:01

    EhCache V2.5 currently offers a solution which can cap based on the memory size of the cache. For more details checkout EhCache 2.5 Documentation

    0 讨论(0)
提交回复
热议问题