【问题标题】:Spring boot Caching with Redis and Store value as Redis HashSpring Boot Caching with Redis and Store value as Redis Hash
【发布时间】:2021-12-28 06:39:02
【问题描述】:

我有一个非常普遍的要求,但很惊讶在互联网上浏览了很多页面后却找不到相同的要求。

下面是代码:

 @Cacheable(value = "cache", key = "#request.userId", cacheManager = "defaultCacheManager")
    public UserDto createOrFetch(CreateUserRequest request) {

我想将返回的 UserDto 对象存储为 Redis Hash,但默认情况下它将它存储为简单的键值对。

  1. 我知道这可以通过我们可以从 Redis 模板获得的 HashOperations 来实现,但它缺乏将 ttl 值设置为哈希值。

  2. RedisRepository 也可以通过在类定义上添加@RedisHash 来使用。

只是想知道是否可以通过@Cacheable 注释方式实现相同的目标。 注意:我正在使用带有 SpringBoot 2.1.4.RELEASE 的 Jedis 客户端

【问题讨论】:

    标签: spring-boot redis spring-data-redis jedis


    【解决方案1】:

    如果我理解正确,您可以使用:

    @Cacheable(value="books", key="T(someType).hash(#isbn)") 
    public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
    

    但是,您如何保证此哈希是唯一的?这是您可能会遇到的问题。

    参考:@Cacheable key on multiple method arguments

    【讨论】:

    • 实际上我想将 UserDto 对象作为 Redis Hash 存储在 Redis 中。 Redis Hash是redis提供的一种数据结构。
    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 2021-07-18
    • 2018-06-06
    • 2022-12-02
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多