1

I am trying to create and delete a cache using RedisCacheManager with spring-boot and want to use HSET programmatically but am unable to do it. I am able to do it as a simple SET but not as HSET.

This is the bean that I have created.

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

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

And even made the class where I am making the call as @RedisHash but no luck.

@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;
    }
}

The o/p I am getting is this when calling these above methods from another @Service class.

    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"

However, I want the o/p like this

    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"

How to achieve this through spring-boot annotations.

Ramish
  • 29
  • 9

1 Answers1

0

It seems not likely to play around Redis Hashes data type using the annotation based way.

In the Cache Abstraction section of Spring Framework documentation, all the usages are based on basic cache operation (get key and set key value, if in the context of Redis)

As you mentioned that you are marking your service class with @RedisHash, though, @RedisHash is used on entities, i.e., the models to be persisted in Redis.

See also

kiz
  • 47
  • 1
  • 8