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