2
votes

I am using spring 4.1 with ehcache. I am able to cache string values with an integer key but whenever I try to cache an object, it fails without any error. The model (POJO) I am saving in cache does implement hashcode, equals and tostring functions.

ehcache configuration as below

<diskStore path="E:/ymserviceslog" />

<defaultCache maxElementsInMemory="50" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />

<cache name="test" maxElementsInMemory="1000" eternal="false"
overflowToDisk="true" memoryStoreEvictionPolicy="LRU"
timeToLiveSeconds="3000" diskPersistent="true" />

Spring configuration as below

<cache:annotation-driven proxy-target-class="true"/>
<context:component-scan base-package="com.ashitosh.ym.dao" />

<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="classpath:ehcache.xml" />
    <property name="shared" value="true"/>
</bean>

<bean id="cachedao" class="com.ashitosh.ym.dao.Cache"/>

My class and method to be cached

public class Cache implements Icache {

    @Override
    @Cacheable(value = "test", key="#id")
    public Party getPerson(int id) {
        Party party = new Party("data",1);
        return party;
    }

}

If I replace the return value of the method getPerson from Party object to String, it works. Any ideas?

1
Your question is as vague as it gets. Are you using JCache + Spring integration? How does your cache configuration looks like? You really need to improve it before anyone is willing to help you out. - Rafal G.
hi R4J, I have modified my question. Hope I am clear now. - Ashitosh
"it fails without any error": Can you be a bit more specific on this? You get an exception but no clear root cause? you never hit the cache? or ?? - Louis Jacomet
hi Louis, The object is not getting cached. Also, I do not get any exception in the log. - Ashitosh

1 Answers

3
votes

I solved it! I needed to implement serializable for object classes which need to be cached.

public class Party implements Serializable {

    private static final long serialVersionUID = 1L;

...