Regarding the use of redis for caching in the spring cacha framework, The default key will have two colons "::"

Viewed 117

Is there any difference between two ":" and one ":", why is "::" used by default

Define key default policy

    String SEPARATOR = "::";
    static CacheKeyPrefix prefixed(String prefix) {
        Assert.notNull(prefix, "Prefix must not be null!");
        return name -> prefix + name + SEPARATOR;}

Custom keys can be set by custom redisConfig

1 Answers

When you implement your caching solution with spring cache framework integrating with Redis, you can add a cache prefix to your key due to the flat nature of Redis storage. Adding a cache prefix is similar to adding namespaces or groups.

Normal cache key -> CACHE_NAME::CACHE_KEY

Cache key with prefix -> CACHE_PREFIX::CACHE_NAME::CAHCE_KEY

I will mention an example below. This is just an example and please apply the concept into your code as required.

  private final CachingProperties cachingProperties;
  private CacheKeyPrefix cachePrefix;

  @PostConstruct
  public void initializeCachePrefix() {
    //Trim the cache prefix if available
    if (!cachingProperties.getCachePrefix().isEmpty()) {
      cachingProperties.setCachePrefix(cachingProperties.getCachePrefix().trim());
      cachePrefix = cacheName -> cachingProperties.getCachePrefix() + "::" + cacheName + "::";
      log.info("Cache prefix set: {}", cachePrefix);
    } else {
      //Prefix is not needed
      log.warn("No cache prefix");
      cachePrefix = CacheKeyPrefix.simple();
    }
  }

cachingProperties -> Instance of a POJO class which contains all the property values for the cache.

cachePrefix -> CacheKeyPrefix instance.

cachingProperties.getCachePrefix() -> The cache prefix planning on adding to the cache.

Finally when you create the CacheManager, then you can add the custom cache prefix as shown below.

  @Bean
  @Primary
  @Order(Ordered.HIGHEST_PRECEDENCE)
  public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    log.info("CacheManager is creating for Redis");
    Duration expiration = Duration.ofSeconds(cachingProperties.getTtlSeconds());
    RedisCacheManager redisCacheManager =
            RedisCacheManager.builder(redisConnectionFactory)
                    .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
                            .disableCachingNullValues()
                            .computePrefixWith(cachePrefix)
                            .serializeValuesWith(RedisSerializationContext.SerializationPair
                                    .fromSerializer(new GenericJackson2JsonRedisSerializer()))
                            .entryTtl(expiration))
                    .build();

    redisCacheManager.setTransactionAware(false);
    log.info("CacheManager (Redis) creation is successful");
    return redisCacheManager;
  }

As you can see the above example, you can set the cachePrefix using the computePrefixWith() method and then custom cache prefix will be applied to the caching framework.

Related