The difference RedisTemplate and StringRedisTemplate

In operation Redis uses RedisTemplate and StringRedisTemplate, but the difference between the two is it?

First: the two respective data access, no interworking

RedisTemplate can not take the data stored StringRedisTemplate

StringRedisTemplate can not take the data RedisTemplate

@Test
    void contextLoads() {
        redisTemplate.opsForValue().set("key1","value1");
        System.out.println("redisTemplate存redisTemplate取:"+redisTemplate.opsForValue().get("key1"));
        System.out.println("redisTemplate存stringRedisTemplate取:"+stringRedisTemplate.opsForValue().get("key1"));

        stringRedisTemplate.opsForValue().set("key2","value2");
        System.out.println("stringRedisTemplate存redisTemplate取:"+redisTemplate.opsForValue().get("key2"));
        System.out.println("stringRedisTemplate存stringRedisTemplate取:"+stringRedisTemplate.opsForValue().get("key2"));

    }

Results are as follows:

redisTemplate deposit redisTemplate taken: VALUE1
redisTemplate deposit stringRedisTemplate taken: null
stringRedisTemplate deposit redisTemplate taken: null
stringRedisTemplate deposit stringRedisTemplate taken: value2

 

Second: different sequences Strategy

RedisTemplate is used by default JDK comes with the serialization strategy

StringRedisTemplate defaults is String serialization strategy

 

StringRedisTemplate inherited RedisTemplate <String, String>

When RedisTemplate first data sequence is stored into the data byte array, and then stored in the database Redis, when we view the data visualization tool string is an array of bytes do not know how

 

 

After StringRedisTemplate stored data, view data by the data visualization tool is readable

 

 

To sum up: If the access of data is the data type String then directly StringRedisTemplate, if it is other object types, and do not want to do any data conversion during removal, it is recommended to use RedisTemplate

Guess you like

Origin www.cnblogs.com/pengpengdeyuan/p/12144585.html