gpt4 book ai didi

java - 在@RestController 上使用 Spring Caching 注解

转载 作者:行者123 更新时间:2023-11-30 07:52:34 25 4
gpt4 key购买 nike

  • 我可以在 RestController 中的方法之上使用 Spring 缓存注释吗?
  • 由于 spring 缓存是一种服务级缓存,但我遇到过没有服务的情况,所以我在 Controller 中使用缓存。
  • 在 Controller 中使用缓存好吗。
  • 我使用咖啡因作为我的底层缓存。
  • 我认为在 Controller 级别使用这种缓存可能会发生任何多线程问题。

最佳答案

我不会在涉及用户身份验证的 spring Controller 中使用 cacheable("something"),因为 cacheable 仅保留参数。但是,如果您不需要根据用户或任何请求 header 信息进行缓存,我看不出有任何问题。

例如,在下面的示例中,当您让用户重新登录时:

        @RequestMapping(...)
@Cacheable("something")
public T analyze(@RequestParam(value = "text") String text) {
....
Object userObj =
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
....
}

如果您想根据 header 进行缓存,您可能有一个选项是在 Controller 方法中添加 @RequestHeader(value="key") String val

关于java - 在@RestController 上使用 Spring Caching 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com