0

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?

Pradeep
  • 850
  • 2
  • 14
  • 27

1 Answers1

2

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.

ragnor
  • 2,498
  • 1
  • 22
  • 25
  • Self invocations (through this) are cached if the class using @Cacheable annotations are proxied with AspectJ using byte-code weaving (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-aj-ltw). Also see (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-understanding-aop-proxies) to understand why the this reference invocations on the target are NOT proxied. – John Blum Nov 19 '15 at 17:26