相关疑难解决方法(0)

Spring Cache @Cacheable - 从同一个bean的另一个方法调用时不工作

从同一个bean的另一个方法调用缓存方法时,Spring缓存不起作用.

这是一个以清晰的方式解释我的问题的例子.

组态:

<cache:annotation-driven cache-manager="myCacheManager" />

<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="myCache" />
</bean>

<!-- Ehcache library setup -->
<bean id="myCache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
    <property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>

<cache name="employeeData" maxElementsInMemory="100"/>  
Run Code Online (Sandbox Code Playgroud)

缓存服务:

@Named("aService")
public class AService {

    @Cacheable("employeeData")
    public List<EmployeeData> getEmployeeData(Date date){
    ..println("Cache is not being used");
    ...
    }

    public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){
        List<EmployeeData> employeeData = getEmployeeData(date);
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:

aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate); 
output: 
aService.getEmployeeEnrichedData(someDate); 
output: Cache is not being used
Run Code Online (Sandbox Code Playgroud)

getEmployeeData方法调用使用缓存employeeData …

java spring caching ehcache

83
推荐指数
6
解决办法
6万
查看次数

标签 统计

caching ×1

ehcache ×1

java ×1

spring ×1