I am running a spring boot 1.5.2 app. I wanted to add caching to my service methods I have added the spring-boot-starter-cache maven dependency and I am using the @ Cacheable annotations but it is not taking effect I created my service beans in a @ Configuration class - here is an example
@Bean(name = "policyService")
public IPolicyService policyService() {
policyService = new PolicyServiceImpl();
return policyService;
}
Here is an example of my service method
@Cacheable(value="policiesCache")
public List<PolicyDBO> findAllPolicies() {
LOG.info("Entered findAllPolicies");
List<PolicyDBO> policyList = policyRespoitory.findAll();
LOG.info("Exiting findAllPolicies");
return policyList;
}
My repoistory interface method is as follows
@Repository
public interface PolicyRepository extends CrudRepository<PolicyDBO, Long>{
/** Find policy by id **/
PolicyDBO findById(Long policyId);
}
Whenever I call this service method from a RestController - the caching is never triggered - it looks like it is not setup properly
Any ideas what I can do to get caching setup correctly?
Thanks Damien
Assuming that caching is active, Spring Caching is working correctly, but not as you expect. @Cacheable caches arguments against results.
In your case, the cache is storing no arguments against the result of List<PolicyDBO>.
However when you call findById, the cache doesn't find anything against an argument of Long and so doesn't return a cached result.