Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

How does RedisCacheManager and Redis manage cache names?

$
0
0

How does Redis manage cache names as used by the getCache("name") method in the org.springframework.cache.CacheManager interface that belongs to the Spring Framework?

I have done a few tests here and could not take any conclusions neither I found any documentation to clarify my question.

I have a simple Spring Boot application that connects to Redis and issues a few commands.

Redis configuration:

@EnableCaching@Configurationpublic class RedisCacheConfig {    @Bean    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();        redisTemplate.setConnectionFactory(connectionFactory);        redisTemplate.setKeySerializer(new StringRedisSerializer());        redisTemplate.setHashKeySerializer(new StringRedisSerializer());        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());        return redisTemplate;    }    @Bean    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {        return (builder) -> {            Map<String, RedisCacheConfiguration> config = new HashMap<>();            RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()              .entryTtl(Duration.ofMinutes(storage.getTtl()))              .disableCachingNullValues();            cacheConfig.usePrefix();            config.put("channel", cacheConfig);            builder.withInitialCacheConfigurations(config);        };    }}

Test Case:

@SpringBootTest@ExtendWith(SpringExtension.class)public class RedisCacheServiceIT {    @Autowired    private RedisTemplate<String, String> redisTemplate;    @Autowired    private RedisCacheManager cacheManager;    @Test    void testCache() {        var cache = cacheManager.getCache("cache1");        cache.put("key1", "value1");        cache.put("key2", "value2");        var cachedVal = cache.get("key1", String.class);        assertEquals("value1", cachedVal);        cache = cacheManager.getCache("channel");        cache.put("xyz", "123");        cachedVal = cache.get("xyz", String.class);        assertEquals("123", cachedVal);        redisTemplate.opsForValue().set("my_cache::xpto", "1234");    }    @Test    void testCacheNames() {        var names = cacheManager.getCacheNames();        names.forEach(n -> System.out.println("cache: " + n));    }}

The above test will print the following line:

  • cache: cache1
  • cache: channel

Even if I manually delete all keys on Redis, next time I run only the test case testCacheNames() without adding any keys to the cache, it also prints the same cache names.

Where does Redis store those cache names? I could not find using a Redis client (i.e. RedisInsight).

Also why "my_cache" was not listed as a cache name as well?


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>