How do we create caches with @Cacheable(cacheName="xyz")

64 views
Skip to first unread message

pankaj tiwari

unread,
Jul 30, 2021, 2:28:43 AM7/30/21
to simple-spring-memecached
I am using SSM with spring-cache and the issue i am facing is i am unable to create cache on fly like @Cacheable(cacheName="xyz") . When i try to do so, i  get "Cannot find cache named 'xyz' for Builder...". 
I understand that there is a cache called "default" but what i want to achieve is to create other caches with this annotation.


@Bean("cacheManager")
public CacheManager cacheManager() throws Exception {

ExtendedSSMCacheManager cacheManager = new ExtendedSSMCacheManager();
Cache cache = this.defaultMemcachedClient().getObject();

cache.getProperties().setUseNameAsKeyPrefix(true);

cacheManager.setCaches(Collections.singletonList(new SSMCache(cache, 0, false)));

return cacheManager;

}


Please advise on same.

Jakub Białek

unread,
Aug 4, 2021, 2:47:50 PM8/4/21
to simple-sprin...@googlegroups.com
Hi,

It is possible to register a new cache on the fly by using SSMCacheManager.addCache() method. When a cache is registered under a given name it can be accessed in @Cacheable annotation.
A several caches can be registered on the startup by using:
cacheManager.setCaches(Arrays.asList(new SSMCache(cache1, 0, false), new SSMCache(cache2, 0, false));
where each cache can be created by using java code that corresponds below XML (example is for xmemcached provider):
<bean name="memcachedClient1" class="com.google.code.ssm.CacheFactory">
    <property name="cacheName" value="myCache1"/>
    <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="127.0.0.1:11211" />
      </bean>
    </property>
    <property name="configuration">
      <bean class="com.google.code.ssm.providers.CacheConfiguration">
        <property name="consistentHashing" value="true" />
      </bean>
    </property>
</bean>
then  @Cacheable(cacheName="myCache1") can be used

-- 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 view this discussion on the web visit https://groups.google.com/d/msgid/simple-spring-memecached/11d9c300-723a-4502-8ea4-428534d4404cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages