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

后端 未结 8 2435
后悔当初
后悔当初 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:46

    How about using a simple LinkedHashMap with LRU algorithm enabled and put all data with a SoftReference in it... such as cache.out(key, new SoftReference(value)) ??

    This would limit your cache to the amount of available memory but not kill the rest of your programm, because Java removes the soft references when there is a memory demand... not all.. the oldest first... usually. If you add a reference queue to your implementation, you can also remove the stall entries (only key, no value) from the map.

    This would free you from calculating the size of the entries and keeping track of the sum.

提交回复
热议问题