Reputation: 423
I'm trying use Echcache
in spring mvc. so for this uses java configuration similar to this:
public net.sf.ehcache.CacheManager ehCacheManager() {
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
CacheConfiguration cacheConfig = new CacheConfiguration();
cacheConfig.setName("test");
cacheConfig.setMaxEntriesLocalHeap(0);
cacheConfig.setMaxEntriesLocalDisk(10);
cacheConfig.setTimeToLiveSeconds(500);
config.addCache(cacheConfig);
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("C:/MyCache1");
config.addDiskStore(diskStoreConfiguration);
return net.sf.ehcache.CacheManager.newInstance(config);
}
and used @cachable
annotation for caching. it works correctly. but all cached data saved in memory. and it create just test.data
file with zero size. Now my queston is how write cached data in disk?
Upvotes: 0
Views: 225
Reputation: 14500
As covered in the other answer, you need to make the cache itself use the disk tier.
If you are using a recent version of Ehcache 2.x, this should be done by:
cacheConfig.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP));
as you indicated in your comment.
Note that this option is a non crash proof disk tier, while the option Strategy.LOCALRESTARTABLE
does handle crashes but only comes in the Enterprise version.
But the above is not sufficient, you need in addition to properly close the cache and cache manager when your application shuts down. This will cause all entries to be flushed and an index file to be created. Without these, the data will be discarded on restart.
Upvotes: 1
Reputation: 195
Add following setting to store in disk:
cacheConfig.setDiskPersistent(true);
public net.sf.ehcache.CacheManager ehCacheManager() {
net.sf.ehcache.config.Configuration config = new
net.sf.ehcache.config.Configuration();
CacheConfiguration cacheConfig = new CacheConfiguration();
cacheConfig.setName("test");
cacheConfig.setMaxEntriesLocalHeap(0);
cacheConfig.setMaxEntriesLocalDisk(10);
cacheConfig.setTimeToLiveSeconds(500);
cacheConfig.setDiskPersistent(true);
config.addCache(cacheConfig);
DiskStoreConfiguration diskStoreConfiguration = new
DiskStoreConfiguration();
diskStoreConfiguration.setPath("C:/MyCache1");
config.addDiskStore(diskStoreConfiguration);
return net.sf.ehcache.CacheManager.newInstance(config);
}
Upvotes: 2