Hi all,
I'm trying to use the Spring caching abstraction to cache some fairly static entities from my Mongo repositories. I think it's pretty straightforward to use Spring's caching when you've got your own concrete classes, but in the case of Spring Data, you only have interfaces, so I need a little advice (pun intended).
My repositories are defined like this:
Here's the syntax that I'm trying to use to cache values returned by count(), exists(ID) & find*(..), but it doesn't look like it's working because I'm seeing database hits every time.
What's the secret incantation to get the Spring Caching abstraction to cache values returned by executions of Spring Data repository methods? I've tried with and without the AspectJ polymorphism operator ("+") to no avail.
Thanks,
Matthew
I'm trying to use the Spring caching abstraction to cache some fairly static entities from my Mongo repositories. I think it's pretty straightforward to use Spring's caching when you've got your own concrete classes, but in the case of Spring Data, you only have interfaces, so I need a little advice (pun intended).
My repositories are defined like this:
Code:
<!-- needs bean: mongoTemplate -->
<mongo:repositories
base-package="app.domain.repo.springdata.mongo"
mongo-template-ref="mongoTemplate"/>
<beans profile="dev,default">
<!-- Needs bean: mongoFactory -->
<bean
id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoFactory"/>
<property
name="writeResultChecking"
value="EXCEPTION"/>
<property
name="writeConcern"
value="JOURNAL_SAFE"/>
</bean>
</beans>
Code:
<bean
id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<bean
id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:META-INF/ehcache.xml"
p:shared="true" />
<bean
id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<bean
id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:META-INF/ehcache.xml"
p:shared="true" />
<cache:advice
id="studyCacheAdvice"
cache-manager="cacheManager">
<cache:caching cache="study">
<cache:cacheable method="count,exists,find*" />
</cache:caching>
</cache:advice>
<cache:advice
id="questionCacheAdvice"
cache-manager="cacheManager">
<cache:caching cache="question">
<cache:cacheable method="count,exists,find*" />
</cache:caching>
</cache:advice>
<cache:advice
id="enumTypeCacheAdvice"
cache-manager="cacheManager">
<cache:caching cache="enumType">
<cache:cacheable method="count,exists,find*" />
</cache:caching>
</cache:advice>
<aop:config>
<aop:advisor
advice-ref="studyCacheAdvice"
pointcut="execution(* app.domain.repo.springdata.mongo.MongoStudyRepository+.*(..))" />
<aop:advisor
advice-ref="questionCacheAdvice"
pointcut="execution(* app.domain.repo.springdata.mongo.MongoQuestionRepository+.*(..))" />
<aop:advisor
advice-ref="enumTypeCacheAdvice"
pointcut="execution(* app.domain.repo.springdata.mongo.MongoEnumTypeRepository+.*(..))" />
</aop:config>
Thanks,
Matthew