0

I want to cache heavy method result, but I for some reason it would not store in cache.

I had tried to use spring cache but it only worked when caching whole api where I only need to cache part of it.

@ResponseBody
public Person validateTest(@Param("name") String name) {
    return cache(name);
}

@Cacheable(value = "getName", key = "#name")
public Person cache(String name) {
    Person person = generateString(name);
    return person;
}


public Person generateString(String name) {
    System.out.println("generating");
    Person person = new Person();
    person.setName(name);
    return person;
}



@Data
class Person implements Serializable { 
   private String name;
}

StackTrace of requests

@EnableCaching is enabled

JDivani
  • 37
  • 5

1 Answers1

0

You're calling the cached method directly here, this doesn't work (the documentation is hardly clear about it). Refactor your code so the @Cacheable method is in a different bean, then inject that bean (which will actually be wrapped in a proxy so it can do the caching), then call it.

john16384
  • 7,800
  • 2
  • 30
  • 44