如何解决Spring 如何存储缓存和 Redis 的密钥
我按照网络上的一些教程使用 redis 设置 Spring Cache,
我的函数是这样的:
@Cacheable(value = "post-single",key = "#id",unless = "#result.shares < 500")
@GetMapping("/{id}")
public Post getPostByID(@PathVariable String id) throws PostNotFoundException {
log.info("get post with id {}",id);
return postService.getPostByID(id);
}
据我所知,@Cacheable
中的值是缓存名称,key
是该缓存名称中的缓存键。我也知道 Redis 是一个内存键/值存储。但是现在我对 Spring 如何将 cache name
存储到 Redis 感到困惑,因为看起来 Redis 只管理键和值,而不是缓存名称。
寻找可以向我解释的人。
提前致谢
解决方法
Spring 在存储数据时使用 cache name
作为键前缀。例如,当您使用 id=1
调用端点时,您将在 Redis 中看到此键
post-single::1
您可以通过 CacheKeyPrefix
类自定义前缀格式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。