0

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?

2 Answers 2

2

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);
}
  • Note: This method is @Deprecated now, and has been replaced with persistence(PersistenceConfiguration) method.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I uses config.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)); but not working.
Strategy.LOCALTEMPSWAP is for in-memory cache only. If you want disk file based then use Strategy.LOCALRESTARTABLE - Note: LOCALRESTARTABLE persistence feature is available in enterprise version of Ehcache.
So why i don't use spring provider for ehcache it writing to the disk?I mean using ehcache method
1

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.LOCALTEMPS‌​WAP)); 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.

1 Comment

Thanks, My problem solved, after i restart the application it's create .index file and .data file. but after setTimeToLiveSeconds expire it delete it an create new file. so as your opinion it's reasonable setTimeToLiveSeconds to 30 days or not? and how handle this problem? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.