Spring cache is not working when calling cached method from another method of the same bean.
Here is an example to explain my problem in clear way.<
In my Case I add variable :
@Autowired
private AService aService;
So I call the getEmployeeData
method by using the aService
@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 = aService.getEmployeeData(date);
...
}
}
It will use the cache in this case.
If you call a cached method from same bean it will be treated as a private method and annotations will be ignored
Use static weaving to create proxy around your bean. In this case even 'internal' methods would work correctly