Memcached @cacheable does not cache

1k Views Asked by At

I am using Simple Spring Memcached (SSM) with my spring boot application. I am new to memcached and am trying to understand things.

For the below code

@RestController
public class TestController {


@RequestMapping(value = "/checkend", method = RequestMethod.GET)
@Cacheable(value="defaultCache")
public String checkInteger(int Id){
    RandomClass r = new RandomClass();
    System.out.println("cache miss...");
    return r.testCache("random");
}
}

public class RandomClass {

@Cacheable(value = "defaultCache")
public String testCache(String randomId){
    System.out.println("came here ");
    return "done1";
}
}  

After a rest call ex: localhost:9000/checkend?Id=7 memcached stores (7 as key, "done1" as value) and will retrieve from cache when the same rest call is made..(note: it does not cache the result for method "testCache" in RandomClass "Why is that?")And even for

@RequestMapping(value = "/checkend", method = RequestMethod.GET)

public String checkInteger(int Id){
    RandomClass r = new RandomClass();
    System.out.println("cache miss...");
    return r.testCache("random");
}
}

public class RandomClass {

@Cacheable(value = "defaultCache")
public String testCache(String randomId){
    System.out.println("came here ");
    return "done1";
}
}  

It does not cache "testCache" method with the given input. Any reason why the method of the RandomClass in this case are not cached?

1

There are 1 best solutions below

1
On

SSM caching annotations work only on Spring beans so change RandomClass to a bean.

It also worth to mention that self invocations (through this) aren't intercepted/cached.