0

I'm using Ehcache to cache the results of an expensive service call within a web application.

I want the cache to persist across JVM restarts.

I configured a 2-tier ehcache with heap and disk. Unfortunately Ehcache only saves the cache to disk when PersistentCacheManager.close() is called.

How can I get the persistence to work properly? Am I supposed to periodically close and reopen the cache? Should I be looking at something other than ehcache for this?

Alex R
  • 11,364
  • 15
  • 100
  • 180
  • Making a cache that can load from a disc store is actually really hard. Likely *Ehcache* decided that storing their cache in memory was good enough. I think you might need to look around more to find what you need. Look for something backed by a database instead of a flat file, because that's basically what you need to if you're going to save objects and retrieve them in real time. (Flat files are just for start-up/shutdown saves.) – markspace Sep 02 '17 at 23:53
  • Your application sounds like a perfect match with SQLite3. – MarsAtomic Sep 02 '17 at 23:59

2 Answers2

0

Ehcache uses a tiered model - data written to the cache is always placed on the lowest tier - disk in your case. Then upon reads happening, frequently accessed values are pulled into the heap tier for faster retrieval. So the data will be written to disk during operations, providing a larger cache space than just heap.

In order to guarantee cache integrity and the ability to recover data after a JVM restart, PersistentCacheManager.close() needs to be called. But that does not mean it has to happen during your application's life, just that you need a proper web application shutdown sequence rather than simply killing the JVM.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
0

I would recommend you to use at Terracotta cluster deployment (it's open source too), next to your web application JVMs.

You would then configure, on your web applications, 3 tiers : heap, offheap and clustered

<cache alias="clustered-cache">
  <resources>
    <heap unit="MB">10</heap> 
    <offheap unit="MB">512</offheap> 
    <clustered-dedicated unit="GB">2</clustered-dedicated> 
  </resources>
</cache>

The Terracotta server deployment would be 1 Active, 1 Passive - so that when you have a failure or a predicated maintenance on 1 server, the other still serves all the clients.

The benefits of using the open source clustered tier, versus the open source disk tier, is that in case of failure (web application container failure, terracotta server failure), your data is still intact, thanks to the redundancy of your setup.

Anthony Dahanne
  • 4,823
  • 2
  • 40
  • 39