Spring Data Repository缓存结果 [英] Spring Data Repository caching results

查看:102
本文介绍了Spring Data Repository缓存结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的spring数据菜鸟。我有如下界面

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);
}

我的缓存配置如下:

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

缓存绝对不起作用。我想这是因为代理的类没有@Cachable批注,但是如何使缓存起作用?

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.

推荐答案

我遇到了同样的问题。您的代码与我的相同。我正在使用eclipseLink,也不得不在persistence.xml上启用缓存。我刚刚添加了一个属性,

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.

这篇关于Spring Data Repository缓存结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆