skip to Main Content

Redis can be used as a cache storage engine within Spring(boot) applications. However, it only allows to set an overall maxmemory size limit. Is there a way to restrict individual RedisCaches in their size, either by number of elements or number of bytes stored in that (sub-)cache?

Is there any wrapper-library that provides such functionality? Or is there perhaps an interface one could implement to easily achieve something like this?

2

Answers


  1. You should write your own redis jar maintaining these restrictions and provide an interface to your main project. This will give you much more flexibility

    Login or Signup to reply.
  2. Yes, you can achieve the same. I would suggest you use the Redisson client for this purpose.

    @Configuration
    @EnableCaching
    public static class Application {
    
        @Bean(destroyMethod="shutdown")
        RedissonClient redisson() throws IOException {
            Config config = new Config();
            config.useClusterServers()
                  .addNodeAddress("redis://127.0.0.1:7004", "redis://127.0.0.1:7001");
            return Redisson.create(config);
        }
    
        @Bean
        public CacheManager cacheManager(RedissonClient redissonClient) {
            Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
    
            // create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
            CacheConfig cacheConfig = new CacheConfig(24*60*1000, 12*60*1000);
            // 100 entries 
            cacheConfig.setMaxSize(100);
            config.put("testMap", cacheConfig);
     
            return new RedissonSpringCacheManager(redissonClient, config);
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search