0

我在我的 Spring Boot 应用程序中使用 Simple Spring Memcached (SSM)。我是 memcached 的新手,正在尝试理解事物。

对于下面的代码

@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";
}
}  

在休息调用 ex 之后: localhost:9000/checkend?Id=7 memcached 存储(7 作为键,“done1”作为值)并将在进行相同的休息调用时从缓存中检索..(注意:它不缓存RandomClass 中方法“testCache”的结果“为什么会这样?”)甚至对于

@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";
}
}  

它不会使用给定的输入缓存“testCache”方法。为什么没有缓存这种情况下的 RandomClass 的方法?

4

1 回答 1

2

SSM 缓存注释仅适用于 Spring bean,因此将 RandomClass 更改为 bean。

还值得一提的是,自我调用(通过this)不会被拦截/缓存。

于 2015-11-18T07:57:10.233 回答