当从同一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"/>
已缓存的服务:
@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);
...
}
}
结果:
aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate);
output:
aService.getEmployeeEnrichedData(someDate);
output: Cache is not being used
getEmployeeData
方法的第二个调用使用了缓存employeeData
,这是预期的。但是当在AService
类中(在getEmployeeEnrichedData
中)调用getEmployeeData
方法时,缓存没有被使用。
这是Spring缓存的工作方式吗?还是我漏掉了什么?
someDate
参数? - Dewfy