【问题标题】:Memcached @cacheable does not cacheMemcached @cacheable 不缓存
【发布时间】:2015-11-18 07:11:13
【问题描述】:

我在 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 stores (7 作为键,“done1”作为值)并且会在进行相同的 rest 调用时从缓存中检索..(注意:它不会缓存 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的方法吗?

【问题讨论】:

    标签: spring-boot memcached spring-cache simple-spring-memcached


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2017-03-29
    • 2016-05-19
    • 2010-10-11
    相关资源
    最近更新 更多