繁体   English   中英

Redis 缓存实现使用 Spring 引导和散列

[英]Redis cache implementation using Spring boot with hashing

我正在尝试使用带有 spring-boot 的 RedisCacheManager 创建和删除缓存,并希望以编程方式使用 HSET 但无法做到。 我可以将其作为简单的 SET 进行,但不能作为 HSET。

这是我创建的 bean。

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() //
        .entryTtl(Duration.ofHours(1)) //
        .disableCachingNullValues();

    return RedisCacheManager.builder(connectionFactory) //
        .cacheDefaults(config) //
        .build();

甚至制作了 class ,我正在以@RedisHash 的身份拨打电话,但没有运气。

@Service
@Slf4j
@RedisHash(value = "CURRENT_CALLS")
public class CacheCleanupService implements Serializable {

    @CacheEvict(value = "CURRENT_CALLS" ,key = "(#cacheKey)")
    public void redisCacheNumberCleanup(String cacheKey) {
        log.info("Key CLEANUP from the cache: {}", cacheKey);
    }

    @Cacheable(value = "CURRENT_CALLS", key = "(#cacheKey)")
    public String redisCacheNumberStore(String cacheKey) {
        log.info("Key Add from the cache: {}", cacheKey);
        return cacheKey;
    }
}

当从另一个 @Service class 调用上述方法时,我得到的 o/p 是这样的。

    127.0.0.1:6379> keys CURRENT_CALLS:*
1) "CURRENT_CALLS::+15109100689:+15134631989"
2) "CURRENT_CALLS::+15109100648:+15134631989"
3) "CURRENT_CALLS::+15109100688:+15134631988"

127.0.0.1:6379> get "CURRENT_CALLS::+15109100648:+15134631989"
"+15109100648:+15134631989"

但是,我想要这样的 o/p

    127.0.0.1:6379> keys CURRENT_CALLS
1) "CURRENT_CALLS"

127.0.0.1:6379> hgetall "CURRENT_CALLS"
1) "+15109100648:+15134631989"
2) "1"
3) "+15109100688:+15134631988"
4) "2"
5) "+15109100689:+15134631989"
6) "3"
7) "+17326667726:+17722915819"
8) "4"

如何通过 spring-boot 注释来实现这一点。

似乎不太可能使用基于注释的方式来处理 Redis 散列数据类型。

在 Spring 框架文档的Cache Abstraction部分中,所有用法都是基于基本的缓存操作(如果在 Redis 的上下文中, get keyset key value

正如您提到的,您正在使用 @RedisHash 标记您的服务@RedisHash ,但是, @RedisHash用于实体,即要在 Redis 中持久化的模型

也可以看看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM