Caching not working

1,144 views
Skip to first unread message

sk

unread,
Jun 30, 2011, 9:09:21 PM6/30/11
to Ehcache Spring Annotations
Hi,

I am using Java 1.5, Spring 3.0.2, ehcache-core-2.0.0 and ehcache-
spring-annotations-1.1.2.
I am trying to implement caching on a method using @Cacheable, but the
caching is not working, the method gets called every time.

@Cacheable(cacheName="myCache", keyGenerator = @KeyGenerator(name =
"StringCacheKeyGenerator")
public String ehCacheTestM(String test)
{
logger.debug("Entered ehCacheTestM");
return (test);
}

As mentioned in one of the earlier posts, I have added it in
interface, and the method is being called in other class.

The context.xml looks like:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/
svn/schema/ehcache-spring"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="file:C:\software\proj\conf\tele\ehcache.xml" /
>


ehcache.xml has an entry:
<cache name="myCache" eternal="false" maxElementsInMemory="100"
overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="10800" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU" />


Is there anything that I am missing, please help.

Thanks.

Eric Dalquist

unread,
Jun 30, 2011, 9:16:02 PM6/30/11
to ehcache-sprin...@googlegroups.com
At this point I'd turn up logging for the com.googlecode.ehcache.annnotations package and see if there are any hints there.

Oh one last thought, how is your application context being loaded? I ask because if you're doing it manually a simple BeanFactory won't work, that doesn't do any of the annotations post processing.

-Eric

Swapnil Upganlawar

unread,
Jul 24, 2012, 2:38:49 PM7/24/12
to ehcache-sprin...@googlegroups.com
Did you ever get answer to this?

I am getting some weird results too. If I add @Cacheable annotation to Controller methods, it's working but if I move this method to one of the helper classes (no signature change, exact same method) caching is not working. I am using Spring 3.1

Eric Dalquist

unread,
Jul 24, 2012, 4:00:19 PM7/24/12
to ehcache-sprin...@googlegroups.com
Are the methods you annotate defined by an interface? If so is your client code only using a reference via the Interface?

Spring does proxying by using an interface based wrapper which is what applies the caching.

-Eric

udoline

unread,
Jul 24, 2012, 5:02:14 PM7/24/12
to ehcache-sprin...@googlegroups.com
Hi,

this could be your problem, then inside "ehcache-spring-1.1.xsd" is an implicit reference to old spring 2.5.

Hint: if you use spring 3.1.x, you should be use the right namespaces ".../schema.../spring-.*-3.1.xsd" in every spring application-context.xml into the whole project.

source "ehcache-spring-1.1.xsd":
<xsd:import namespace="http://www.springframework.org/schema/tool"
schemaLocation="http://www.springframework.org/schema/tool/spring-tool-2.5.xsd"/>

--
bye  udoline

Ella Baila Sola

unread,
Jul 27, 2012, 6:04:41 AM7/27/12
to ehcache-sprin...@googlegroups.com
Hi

If using Spring 3.0, or better 3.1, forgot Spring annotations for ehcache and use Spring caching. I think this google project won't last too much. It works fine, but the Spring feature committment is stronger than google-code one.

Notice that the "clever" Spring people has decided that the default key generator for the caching does not take into account the function name. So... If you have two functions with the same argument signature to be cached in the same cache, they will clash. You will have to create your own key generator.

this in the app context file:
<cache:annotation-driven key-generator="keyCacheGenerator" />
<bean id="keyCacheGenerator" class="liba.KeyCacheGenerator" />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="default" />
                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="imex" />
            </set>
        </property>
</bean>

and this is one possible key generator source:

package liba;

import java.lang.reflect.Method;

import org.springframework.cache.interceptor.KeyGenerator;

public class KeyCacheGenerator implements KeyGenerator {

    @Override
    public Object generate(Object arg0, Method arg1, Object... arg2) {
        String a = arg1.getName() + "@;#";
        for (Object o : arg2) {
            if (o == null)
                a += "&nulo&";
            else {
                a += o.getClass().getName() + "$" + o.toString();
            }
            a += "--.";
        }
        return a;
    }

}

Regards. And don't forget to follow the Spring instructions about caching support


2012/7/24 udoline <udo.k...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages