Skip to content
Advertisement

SpringBoot cache not working for non parameter methods

I have one spring boot application

pom.xml

JavaScript

CacheDemoAllpication.java

JavaScript

Controller

JavaScript

Service

JavaScript

In the service class getBookMap() return a Map and I want spring to cache that. For subsequent requests it should return that map from cache. But with current setup It’s not caching anything and getBookMap() method is being executed every time. My first suspect was spring-boot-starter-cache version, but that’s not the case. The behavior is same with latest version as well.

I have tried with/without key=”#root.methodName” but the result is same.

Whereas if I cache method getBook(Integer id) it works just fine which gets me into thinking that no parameter methods are behaving differently.

Advertisement

Answer

  1. @Cacheable won’t work for private methods, make your method public
  2. You can’t call a cacheable method from within the same class. I mean put your Cacheable method into another class and make it public. Call the method from another class. Then it’ll work
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement