cancel
Showing results for 
Search instead for 
Did you mean: 

Does @Cachable work in Hybris?

Former Member
0 Kudos
1,551

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?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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?

former_member1320010
Participant
0 Kudos

We only had a simple implementation of an interface where we used cachable :)

Answers (1)

Answers (1)

former_member1320010
Participant
0 Kudos

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>

Former Member
0 Kudos

Can you please specify the exact location of ehcache.xml?

Thanks, Ridhi

former_member1320010
Participant
0 Kudos

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>
 
Former Member
0 Kudos

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?

former_member1320010
Participant
0 Kudos

i use the pk as my key in this case @Cacheable(value = "myUserCache", key = "#userModel.getPk()", unless = "#userModel == null")