【问题标题】:Cache response of spring rest api controllerspring rest api控制器的缓存响应
【发布时间】:2018-09-12 17:49:58
【问题描述】:

有没有办法通过方法参数缓存spring REST API的响应?例如,在下面的代码中,如果国家/地区已经检索过一次,则从缓存中返回相同的响应(json 序列化数据)。

@Controller
public class DataController {

      // Can we cache here by country?
     @RequestMapping(value = "/api/info/{country}", method = RequestMethod.GET)
     public CountryInfo getCountryInfo(@PathVariable("country")String country){
          return service.getCountryInfo(country);
     }
}

【问题讨论】:

  • 你可以,不过最好加个Spring缓存机制,在service方法里加@Cacheable就行了。
  • @daniu 为什么?在控制器中缓存,我希望还能节省序列化成本。
  • 在到达您发布的代码时,序列化仍然会发生(即将生成的CountryInfo 编码为 JSON)。

标签: java spring-mvc caching


【解决方案1】:

有一个很好的guide 展示了如何使用 Spring 缓存数据。

即使我的答案不包含任何代码,我还是建议您看一下。这是一个分步指南,我相信它可以帮助您解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多