Ashitosh
Ashitosh

Reputation: 365

Spring with ehcache not caching objects

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?

Upvotes: 2

Views: 2805

Answers (1)

Ashitosh
Ashitosh

Reputation: 365

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;

...

Upvotes: 4

Related Questions