Spring кеш не работает при вызове кэшированного метода из другого метода того же bean.
Вот пример, чтобы объяснить мою проблему ясным образом.
Конфигурация:
<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
во втором вызове, как ожидалось. Но когда метод getEmployeeData
вызывается в классе AService
(в getEmployeeEnrichedData
), кеш не используется.
Является ли это тем, как работает кеш spring или я что-то не хватает?