Announcement Announcement Module
Collapse
No announcement yet.
Pointcut syntax for properly applying Spring Caching abstraction to SD repositories? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointcut syntax for properly applying Spring Caching abstraction to SD repositories?

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

  • #2
    [bump] I've tried too many different variations to list. Anyone have any ideas?

    Comment


    • #3
      Is there any chance you can share some sample code to debug whats going on? Generally this should work as expected. There's a module in the Spring Data JPA Examples repository [0] setting up an additional Advisor to wrap the repositories (in JavaConfig, but XML should just work the same). Generally the repositories should just work as expected as they're plain Spring beans and undergo the same bean lifecycle methods as all other beans.

      You could also try redeclaring the methods that you'd like to cache in the repository interfaces and apply @Cacheable to them.

      [0] https://github.com/SpringSource/spri...a-interceptors

      Comment


      • #4
        Originally posted by Oliver Gierke View Post
        Is there any chance you can share some sample code to debug whats going on? (snip...)
        As soon as I'm done making everything functional in my app, I'll take one of the SD sample apps on github & try to duplicate. Won't be for a few days, though, as we can work ok for the next couple of iterations just fine without caching...

        Comment

        Working...
        X