Spring cache non funziona quando si chiama un metodo memorizzato nella cache da un altro metodo dello stesso bean.
Ecco un esempio per spiegare in modo chiaro il mio problema.
Configurazione:
<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"/>
Servizio memorizzato nella cache:
@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);
...
}
}
Risultato:
aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate);
output:
aService.getEmployeeEnrichedData(someDate);
output: Cache is not being used
La getEmployeeDatachiamata al metodo utilizza la cache employeeDatanella seconda chiamata come previsto. Ma quando il getEmployeeDatametodo viene chiamato all'interno della AServiceclasse (in getEmployeeEnrichedData), Cache non viene utilizzato.
È così che funziona Spring cache o mi manca qualcosa?
someDateparam?