RedisCacheWriter和RedisTemplate之间有什么区别

xqkwcwgp  于 2022-10-31  发布在  Redis
关注(0)|答案(1)|浏览(449)
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}

第一种情况:使用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;
}

案例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;
}

RedisCacheManagerRedisTemplate之间有什么区别?这两个类之间的内存消耗有很大的区别。是不是与它们在缓存中存储的内部实现有关?
Springboot 2.x有RedisCacheWriter,但在Springboot 1.4.3中,我们必须使用RedisTemplate

oyt4ldly

oyt4ldly1#

RedisCacheWriter磁盘缓存编写器

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.
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.

推荐模板

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

RedisCache管理器

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.

相关问题