memcached with @Cacheable is not working

315 views
Skip to first unread message

kishore....@gmail.com

unread,
Nov 29, 2013, 8:30:26 AM11/29/13
to simple-sprin...@googlegroups.com

Hi All,

I am a newbie to memcache. I have a webservice which will return objects in json format, that one I would like to cache it.I have the following configuration in my application. Though I am not getting any error/exception, doesn't look like it is working because I can see the generated hql in the logs as many times I am invoking this service.


I have elastic cache cluster available in Amazone cloude. This is nothing but memcache managed by Amazone. Can I replace this address with DefaultAddressProvider's address?



<context:annotation-config />

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
         <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
        <property name="caches">
            <set>
                <bean class="com.google.code.ssm.spring.SSMCache">
                    <constructor-arg name="cache" index="0" ref="defaultCache"/>
                    <constructor-arg name="expiration" index="1" value="300"/>
                    <constructor-arg name="allowClear" index="2" value="false"/>
                </bean>
            </set>
        </property>
    </bean>

    <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
        <property name="cacheName" value="defaultCache"/>
        <property name="cacheClientFactory">
            <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/>
        </property>
        <property name="addressProvider">
            <bean class="com.google.code.ssm.config.DefaultAddressProvider">
                <property name="address" value="cloudadress:11211"/>
            </bean>
        </property>
        <property name="configuration">
            <bean class="com.google.code.ssm.providers.CacheConfiguration">
                <property name="consistentHashing" value="true"/>
            </bean>
        </property>
    </bean>


I have added the @Cacheable at method level in one of my Controller class, that invokes db through hibernate
    @RequestMapping(method = RequestMethod.GET, value = "/{ids}")
    @ResponseBody
    @EnableApiRequestResponseLogging
    @Cacheable(value = "defaultCache", key = "#ids")
    public ResponseEntity<List<MyModel>> read(@PathVariable("ids") String ids, final HttpServletRequest request) throws ServiceException {}

Can someone help me out on this?

Regards,
Kishor

Jakub Białek

unread,
Nov 29, 2013, 9:27:52 AM11/29/13
to simple-sprin...@googlegroups.com
Hi,

It should be possible to use and configure Amazon elastic search using DefaultAddressProvider, just provide hostname (or ip) and port number.

Regarding the cache issue I think it because how Spring is using proxy, here you can read a nice article that explains it: http://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring.
In general you shouldn't use @Cacheable at controller level instead:
- use @Cacheable on your services (beans that are used by controllers)
- if you want to cache controller's response (http response) use dedicated software: Varnish it will be a lot faster than caching in Java code (no matter what cache backend is used: memcached, ehcache, infinispan, hashmap).

Best regards,
-- Ragnor


--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-meme...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at http://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/groups/opt_out.

kishore....@gmail.com

unread,
Dec 2, 2013, 5:50:04 AM12/2/13
to simple-sprin...@googlegroups.com
Hi Ragnor,

Thanks for your post. I had gone thru the link which you mentioned in the previous post. As you mentioned I put @Cacheable in my service class, then I was getting
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class

Once I added default constructor in it, then it was not throwing any exception but still it is not caching.

I even changed the defaultaddressprovider's address to some another address which is not existing. Still it is not
throwing any exception. Looks like spring framework is ignoring the @Cacheable annotaion.

Regards,
Kishor
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.

kishore....@gmail.com

unread,
Dec 3, 2013, 1:12:58 AM12/3/13
to simple-sprin...@googlegroups.com
it was the connection issue. it was not able to connect to the memcache server. So in this case it should throw some exception, rt?

Kishor

Jakub Białek

unread,
Dec 3, 2013, 2:11:30 AM12/3/13
to simple-sprin...@googlegroups.com
Could you please show your Spring's xml configuration files?
You can look at http://code.google.com/p/simple-spring-memcached/source/browse/#svn%2Ftrunk%2Fspring-cache-integration-test to see working example of Spring Cache and SSM. As you can see the application-context.xml should contain:
<cache:annotation-driven />

<context:annotation-config />

<context:component-scan base-package="com.mypackage" />

-- Ragnor




To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-meme...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages