Bộ đệm mùa xuân không hoạt động khi gọi phương thức được lưu trong bộ nhớ cache từ một phương thức khác của cùng một bean.
Đây là một ví dụ để giải thích vấn đề của tôi một cách rõ ràng.
Cấu hình:
<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"/>
Dịch vụ lưu trong bộ nhớ 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);
...
}
}
Kết quả :
aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate);
output:
aService.getEmployeeEnrichedData(someDate);
output: Cache is not being used
Cuộc getEmployeeDatagọi phương thức sử dụng bộ nhớ cache employeeDatatrong cuộc gọi thứ hai như mong đợi. Nhưng khi getEmployeeDataphương thức được gọi trong AServicelớp (in getEmployeeEnrichedData), Cache sẽ không được sử dụng.
Đây có phải là cách bộ đệm mùa xuân hoạt động hay tôi đang thiếu thứ gì đó?
someDatetham số không?