【问题标题】:How Spring store cache and key to RedisSpring 如何将缓存和密钥存储到 Redis
【发布时间】:2021-04-24 14:20:18
【问题描述】:

我按照 web 上的一些教程使用 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 只管理键和值,而不是缓存名称。

寻找可以向我解释的人。

提前致谢

【问题讨论】:

    标签: redis spring-cache


    【解决方案1】:

    Spring 在存储数据时使用cache name 作为键前缀。例如,当您使用 id=1 调用端点时,您将在 Redis 中看到此键

    post-single::1
    

    您可以通过CacheKeyPrefix类自定义前缀格式。

    【讨论】:

    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多