简体   繁体   中英

Memcached @cacheable does not cache

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM