Spring Data Repository caching results

只愿长相守 提交于 2020-01-03 04:50:11

问题


I am a complete spring data noob. I have an interface as follows

public interface UserBalanceRepository extends PagingAndSortingRepository<UserBalance, Integer>
{
    @Cachable("UserList")
    @Query("select userId from UserBalance")
    List<Integer> ListUserIds(Pageable pageable);
}

My cache configuration looks like this:

<cache:annotation-driven />

<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
  <property name="caches">
    <set>
      <bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="UserList"/>
    </set>
  </property>
</bean>

The caching does absolutely nothing. I guess it is because the proxied class does not have the @Cachable annotation, but how do I make the caching work? Is there a different way to do caching?

My last resort will be to put the calls that need to be cached inside a wrapper class and cache there.


回答1:


I was facing the same issue. Your code is the same with mine. I am using eclipseLink and i had to enable caching on persistence.xml too. I just added a property,

        [...]
        <class>com.project.web.model.entity.ReturnOrder</class>
        <class>com.project.web.model.entity.BillingAddress</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://connection string"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.password" value="pass"/>

            <!-- PROPERTY ADDED -->
            <property name="eclipselink.cache.shared.default" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Also, this is a very good tutorial.



来源:https://stackoverflow.com/questions/17896118/spring-data-repository-caching-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!