Setting up multiple cache with ssm spring cache memcache setup

586 views
Skip to first unread message

Hariharan Visvanathan

unread,
Jan 16, 2015, 11:24:43 AM1/16/15
to simple-sprin...@googlegroups.com
Hi
iam trying to get method level caching on my service layer and i want to use it by annotation only and not having put a xml entry to every cache name.

i was able to get the setup working for a single cache

but looks like if i have to add a new cache i have to put a entry in the Set for each in in cachemanager bean.
is there a way to drive this fully from annotation.
if i just add a annotaion @cacheable i get cache not found

like if i have findById and findBySubjectId methods looks like i need two ssmcache instances and both have to be declared in the SET for cachemanager bean.

am i missing something.

i was hoping to drive everything by annotation
can you please help


--- this is my config

    <cache:annotation-driven />


    <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="${memcached.timeout}" />
                </bean>
                <bean class="com.google.code.ssm.spring.SSMCache">
                    <constructor-arg name="cache" index="0" ref="findById" />
                    <constructor-arg name="expiration" index="1" value="${memcached.timeout}" />
                </bean>
            </set>
        </property>
    </bean>

    <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
        <property name="cacheName" value="default" />
        <property name="cacheClientFactory" ref="myCacheClientFactory" />
        <property name="addressProvider" ref="myCacheAddressProvider" />
        <property name="configuration" ref="myCacheConfiguration" />
    </bean>

    <bean name="findById" class="com.google.code.ssm.CacheFactory">
        <property name="cacheName" value="findById" />
        <property name="cacheClientFactory" ref="myCacheClientFactory" />
        <property name="addressProvider" ref="myCacheAddressProvider" />
        <property name="configuration" ref="myCacheConfiguration" />
    </bean>

    <bean name="myCacheClientFactory" class="com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl" />


    <bean name="myCacheAddressProvider" class="com.google.code.ssm.config.DefaultAddressProvider">
        <property name="address" value="${memcached.nodeAddresses}" />
    </bean>
    <bean name="myCacheConfiguration" class="com.google.code.ssm.providers.CacheConfiguration">
        <property name="consistentHashing" value="true" />
        <property name="useBinaryProtocol" value="true" />
    </bean>

Jakub Białek

unread,
Jan 16, 2015, 4:28:56 PM1/16/15
to simple-sprin...@googlegroups.com

Hi,

Could you show how you use it in your code?
You can use one ssmcache for many cached metody.

__ 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/d/optout.

Hariharan Visvanathan

unread,
Jan 16, 2015, 5:17:51 PM1/16/15
to simple-sprin...@googlegroups.com
thanks ragnor
you are right i think i should use it as  just as a key value pair with one SSMCache instance
i got confused as if like entity cache types.
like for example i have following methods say in my interface

i just started playing around.
the only problem i have is with keygeneration
would have any idea if spring el supports like method name plus parameter id concat 
or i have to write a keygenerator class.



public interface BookManager {
@Cacheable(value="defaultCache",key="#id")
List<Book> getBooksBySubjectId(int topicId) throws DataAccessException, ValidationException;

@Cacheable(value="defaultCache",key="#id")
Book findById(String id) throws ValidationException, DataAccessException;

}

public interface BookEditionManager {
@Cacheable(value="defaultCache",key="#id")
    public List<BookEdition> findBookEditionsByBookId(String id) throws DataAccessException, ValidationException;

@Cacheable(value="defaultCache",key="#id")   
public List<BookEdition> findBookEditionsByAuthorId(String id) throws DataAccessException, ValidationException;

}






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

Jakub Białek

unread,
Jan 17, 2015, 7:51:31 AM1/17/15
to simple-sprin...@googlegroups.com
According to this issue: https://jira.spring.io/browse/SPR-8933 by default method name is not used in generated cache key, only method's paramters are taken into account.

I think that one of SSM feature can help you: https://github.com/ragnor/simple-spring-memcached/issues/21. One cache can have mutiple aliases and be exposed to Spring under different names. The name will be used as a part of generated cache.

-- 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