I am working with ehcache. I am caching Spring @Service method :
@Service( value = "dataServicesManager" )
@Transactional
public class DataServicesManager implements IDataServicesManager{
@Autowired
private IDataDAO dataDAO;
@Override
@Cacheable( value = "alldatas" )
public List<Data> getAllDatas(Integer param) {
// my logic
return results;
}
// others services
}
Here is the Spring configuration snippet:
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
Here is my ehcache configuration.
<ehcache xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<diskStore path="C:/TEMP/ehcache"/>
<defaultCache eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="1200"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
When i call the service method getAllDatas from a Spring @Controller the method is cached and the second time call retrieve the result stores in cache.
What i don't understand is that i cannot find the <diskStore path="C:/TEMP/ehcache"/>
specify in the ehcache.xml. So i have two questions :
Question 1: Why "C:/TEMP/ehcache" directory is not created ?
Question 2: Where is cached my service results ?
Your Ehcache configuration is to blame.
The defaultCache
element will only be used when you create a cache programatically without specifying a configuration.
But you define explicitly your alldatas
cache, without any disk options.
So your configuration needs to become:
<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</cache>
And then this cache will use the disk store.
If you do not plan on having other caches in your application, you can also remove the defaultCache
element for clarity.