hack
hack

Reputation: 1603

Spring ehcache - Cache not being retrieved from disk

Requirement: While caching, if max heap size provided for caching exceeds, it should overflow to disk and fetch it from there. My ehcache configuration:

cache:
timeToLiveSeconds: 3600
ehcache:
 maxBytesLocalHeap: 1M
 setMaxBytesLocalDisk: 5G
 diskStore: cacheDiskStore

This is ehcache.xml configuration (which is over ridden by values provided in yml):

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" name="CM1"
updateCheck="false" maxBytesLocalHeap="1M" maxBytesLocalDisk="1M" maxEntriesLocalDisk="100000">

<!-- This is a default configuration, it is re-configured by the CacheConfiguration 
    Spring Bean, using the properties from the resources/config/*.yml files. -->

<diskStore path="cacheDiskStore" />

<defaultCache eternal="false" overflowToDisk="true" />
<cache name="cache1" >
    <persistence strategy="localTempSwap" /> 
</cache>
<cache name="cache2" >
    <persistence strategy="localTempSwap" /> 
</cache>

I am caching large media files. But when i try to retrieve Element from cache, it always comes as null. This is how i am retrieving from cache:

Element elt = CacheManager.getInstance()
            .getEhcache("<cacheName>").get(<key>);
    if (elt != null) {
        System.out.println("**** Retrieved from cache *****");
        return elt;
    } else {
        System.out.println("**** NOT FOUND IN CACHE *****");
        return null;
    }

It is not picking from cache. If i increase cache heap size, it works fine. Any help?

Upvotes: 0

Views: 615

Answers (1)

Louis Jacomet
Louis Jacomet

Reputation: 14500

In Ehcache 2.x, the disk store still consumes heap for the keys. So it is totally possible that some of your cache content gets evicted because between the keys of mappings held on disk and the mappings on heap, you end up causing eviction.

Note also that the open source disk store will only recover data on restart if there was a clean shutdown of the cache manager when the application exits. That is CacheManager.shutdown() must be invoked.

Upvotes: 1

Related Questions