小编Cha*_*hea的帖子

如何在 Spring Boot 中使用控制器/服务上的缓存?

我正在尝试按照https://www.java4s.com/spring-boot-tutorials/how-to-configure-cache-in-spring-boot-applications/ 上的说明在我的 spring-boot 应用程序上添加缓存,但它不起作用。我不完全确定如何测试它。我在控制器下有一个 system.out.print 就像这篇文章一样。如果缓存有效,那么它只会打印“测试”一次,但从具有相同输入的请求中返回相同的结果。我的代码如下:

货币控制器.java

    @RequestMapping(method = RequestMethod.POST)
    @Cacheable(value="currency")
    public ResponseEntity getExchangedCurrency(final @RequestBody CurrencyExchange currencyExchange) {
        System.out.println("Test");
        return ResponseEntity.ok()
              .headers(responseHeaders)
              .body(currencyService.calculate(currencyExchange));
    }

Run Code Online (Sandbox Code Playgroud)

应用程序.java

@SpringBootApplication
@EnableCaching
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);



    }
}

Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-cache

3
推荐指数
1
解决办法
3104
查看次数

标签 统计

java ×1

spring-boot ×1

spring-cache ×1