Richie
Richie

Reputation: 5199

Spring annotation-driven ehcache note evicting using timeToLiveSeconds

Sorry in advance for the long post however I wanted to be precise in my post.

I have a spring mvc 3.1 web application and have just built ehcache into my web application which I am using to cache all my list of values (drop downs) built from the database.

Here is an example of my settings...

    <!-- Cache -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.5.0</version>
    </dependency>

...

<ehcache>
    <diskStore path="java.io.tmpdir"/>      
    <cache
        name="lovStore"
        maxElementsInMemory="512"
        eternal="false"
        timeToIdleSeconds="60"
        timeToLiveSeconds="60"
        overflowToDisk="false"
        memoryStoreEvictionPolicy="LRU"/>                       
</ehcache>

...

<cache:annotation-driven />
    <bean id="cacheManager" 
        class="org.springframework.cache.ehcache.EhCacheCacheManager" 
        p:cache-manager-ref="ehcache"/>
    <bean id="ehcache" 
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
        p:config-location="classpath:ehcache.xml"/>

...

@Cacheable(value = "lovStore", key = "#code")
public Map<String, String> getLov(String code) throws ReportingManagerException {
    return MockLOVHelper.getInstance().getLov(code);
}   

A lot of the tutorials on the net talk about evicting the cache using a @cacheEvict method however this does not suit me. I think I am better suited off using the timeToLiveSeconds option to evict the cache.

When i look in the logs the cache is definitely working however the eviction of the cache is not. I've read some other articles on the net about how timeToLiveSeconds doesn't truly evict the cache but other articles like this one http://code.google.com/p/ehcache-spring-annotations/wiki/EvictExpiredElements which say there are special settings you have to create to get the cache to evict.

Can someone please help me understand if my cache should be evicting and also how I can evict because what is mentioned in the article is not something i was able to understand how to implement.

Here are what my logs look like. But there are no signs of eviction...

2014-01-20 13:32:41,791 DEBUG [AnnotationCacheOperationSource] - Adding cacheable method 'getLov' with attribute: [CacheableOperation[public java.util.Map com.myer.reporting.dao.mock.MockLovStoreDaoImpl.getLov(java.lang.String) throws com.myer.reporting.exception.ReportingManagerException] caches=[lovStore] | condition='' | key='#code']

thanks

Upvotes: 0

Views: 1211

Answers (1)

Arunas Junevicius
Arunas Junevicius

Reputation: 747

Ecache does not evict elements until there's need to do that. And that's reasonable since there is no need to waste CPU resources for operation(evicting elemets) that wouldn't make much difference.
If someone would try to get element from cache and the element's time to live/idle would be expired, then ehcache would evict that element and return null. In case max elements or memory for cache/pool would be reached then ehcache would evict elements that are expired, based on eviction policy.
And if I understand correctly it's not guaranteed that all expired element's would be evicted since only sample elements are selected to evict(from documentation):

@param sampledElements this should be a random subset of the population

Upvotes: 1

Related Questions