2

I am evaluating cache for my project. cache2k looks very promising. I went over the documentation, but could not find following items:

  • can cache2k overflow to disk?

  • can cache2k evict the entries (or overflow to disk) based on memory size (in bytes) based limit?

bdhan
  • 21
  • 1

1 Answers1

1

can cache2k evict the entries (or overflow to disk) based on memory size (in bytes) based limit?

In a few days I will release version 1.4 which includes support for a Weigher. With that you can provide a method that gets called whenever an entry is created or updated, which returns a weight of the entry, for example reflecting its approximate size on the heap. This includes the possibility to wire in libraries that estimate the memory size of an entry, which would provide what you asked for.

Every library that estimates the size needs to navigate through the object references. This is costly and might fail in case some global objects are referenced. For example referenced String object might be interned.

In some cases a weigher is very easy to implement. For example if images with byte arrays are cached, the weigher can just return the array size.

In case your cache entries are similar in size I recommend not using the weigher. Even a simple weigher implementation, does incur overhead. With a weigher present an entry update leads to an update of a global data (the total weight in the cache) and needs additional locking and synchronization.

can cache2k overflow to disk?

That was present in versions prior to 1.0 but removed in a cleanup to focus on a pure heap based cache. However, the overall design of the API and internal structure still keeps that in mind, so the feature could be added again. At some point that might happen, but not soon.

cruftex
  • 5,545
  • 2
  • 20
  • 36
  • Thanks @cruftex for very detailed answer. – bdhan Aug 17 '20 at 17:18
  • Thanks @cruftex for very detailed answer. We will certainly look forward for it. As per some of the published reports, cache2k has shown much better performance compared to other well known libraries. We want to use memory calculation to understand the space usage pattern, hence surely wont be used in production. We will use collected data to load cache, and see the usage pattern. But, eviction based on memory size is a requirement from consumers, and we would definitely be required to support it. – bdhan Aug 18 '20 at 07:30