on ‎2018 Nov 05 5:56 PM
Hi to all. I have tried to add @org.springframework.cache.annotation.Cacheable in core extension for method something like this
public class SomeClass implementes SomeInterface {
@Cacheable(value = "plantsCache", key = "T(org.springframework.cache.interceptor.SimpleKeyGenerator).generateKey(#baseStore.uid)")
public SomeData getSomeData(@Nonnull final BaseStoreModel baseStore) {
...
}
}
And spring configuration:
<cache:annotation-driven cache-manager="compositeWhrCoreCacheManager"/>
<utils:list id="defaultWhrCoreCacheManagerList">
<ref bean="whrCoreCacheManager"/>
</utils:list>
<alias name="defaultCompositeWhrCoreCacheManager" alias="compositeWhrCoreCacheManager"/>
<bean id="defaultCompositeWhrCoreCacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<ref bean="whrCoreCacheManagerList"/>
</property>
</bean>
<alias name="defaultWhrCoreCacheManager" alias="whrCoreCacheManager"/>
<bean id="defaultWhrCoreCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="whrCoreEhcache"/>
</bean>
<alias name="defaultWhrCoreEhcache" alias="whrCoreEhcache"/>
<bean id="defaultWhrCoreEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/mycore/cache/ehcache.xml"/>
<property name="cacheManagerName" value="whrCoreCache"/>
</bean>
In some class
private SomeInterface instanceFromSpringContext;
When instanceFromSpringContext.getSomeData(baseStore) is called again then the real call still is executed, and what is interesting the bean is not proxied. The instance still directly has an implementation of SomeClass. Also, I have tried not annotation based mechanism when
<cache:advice></<cache:advice>
elements are configured, but the issue is still the same, looks like the root is somewhere in another place. Does anyone have experience of adding @Cachable for methods of classes which implement interfaces?
Help others by sharing your knowledge.
AnswerRequest clarification before answering.
Thanks . The issue was resolved, maybe it will be interested:
Bean A has methods with annotation @Cachable. Bean B a has dependency on Bean A and also Bean B implements org.springframework.beans.factory.FactoryBean and Spring inits Bean B before enabling caching(when proxies are created for beans which methods have annotation @Cacheable).
Santa Barbara 🙂
I'm interesting is it Spring issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ,
We were able to implement this by the below configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
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.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">;
<!-- enables scanning for @Cacheable annotation -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" >
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="ehcache.xml" p:acceptExisting="true" />
<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="ehcache" />
<property name="cacheName" value="myCache" />
</bean>
</beans>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
extension/resources/cache
i had ehcache.xml here and one more spring config as below . and i included this spring in my extension spring (just to seperate it in a clean way)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
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.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">;
<!-- enables scanning for @Cacheable annotation -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" >
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="ehcache.xml" p:acceptExisting="true" />
<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="ehcache" />
<property name="cacheName" value="myCache" />
</bean>
</beans>
I am using @Cacheable(value = "productRecommendationCache", key = "#categoryId, #segmentId")
Cannot find cache named 'productRecommendationCache' for Builder[public java.util.Map com.maf.core.product.impl.DefaultMafProductRecommendationService.getProductRecommendationsForHomePage(java.lang.String,java.lang.String,java.lang.String)] caches=[productRecommendationCache] | key='#categoryId, #segmentId' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
Can you suggest if you are using custom keygenerator or using default one?
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.