What is the difference between RedisCacheWriter and RedisTemplate

Viewed 1553
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}

case 1: Using RedisCacheWriter

public RedisCacheManager getRedisCacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheWriter cacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
    ClassLoader loader = this.getClass().getClassLoader();
    JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader);
    SerializationPair<Object> pair = SerializationPair.fromSerializer(jdkSerializer);

    RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
    cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(3600));


    Map<String, RedisCacheConfiguration> initialCacheConfigurations = new HashMap<>();
    initialCacheConfigurations.put("order",cacheConfig.entryTtl(Duration.ofSeconds(1600)));


    RedisCacheManager cacheManager = new RedisCacheManager(cacheWriter, cacheConfig,initialCacheConfigurations);

    return cacheManager;
}

@Bean
public CacheManager cacheManager() {
    RedisCacheManager cacheManager=getRedisCacheManager(jedisConnectionFactory());
    log.info("CacheManager created ");
    return cacheManager;
}

case 2: RedisTemplate

@Bean
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    return redisTemplate;
}

@Bean
CacheManager cacheManager() {

    //JedisConnectionFactory cacheManager=jedisConnectionFactory();
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
    cacheManager.setDefaultExpiration(60);
    cacheManager.setUsePrefix(true);            
    return cacheManager;
}

What is the difference between RedisCacheManager and RedisTemplate? There is a significant memory consumption difference between both the classes. Is it something regarding their internal implementation of storing in cache?
Springboot 2.x is having RedisCacheWriter but in Springboot 1.4.3 we have to use RedisTemplate.

1 Answers

RedisCacheWriter

RedisCacheWriter offers low level access to caching-related Redis operations (SET, SETNX, GET, EXPIRE, etc.).

The RedisCacheWriter is responsible for writing and reading binary data into and out of Redis, and it may be shared by different cache implementations. Possible cache lock flags are respected by the implementation.

RedisTemplate

Automatic serialization and deserialization between the provided objects and the underlying binary data in the Redis store is carried out by RedisTemplate.

RedisCacheManager

Caches are automatically created by this cache manager upon initial write.

Due to the way Redis displays empty data structures, empty caches are not accessible on Redis.

RedisCacheManager enables the specification of caches needing a different RedisCacheConfiguration than the predefined configuration.

Related