1. The relationship between the two is that StringRedisTemplate inherits RedisTemplate.
2. The data of the two are not common; that is to say, StringRedisTemplate can only manage the data in StringRedisTemplate, and RedisTemplate can only manage data in RedisTemplate.
3. There are two serialization strategies adopted by SDR by default, one is the serialization strategy of String, and the other is the serialization strategy of JDK.
StringRedisTemplate adopts the serialization strategy of String by default, and the saved key and value are serialized and saved using this strategy.
RedisTemplate uses the JDK serialization strategy by default, and the saved key s and value s are serialized and saved using this strategy.
The default sequence class used by RedisTemplate is when operating data. For example, when storing data, the data will be serialized into a byte array first and then stored in the Redis database. When you open Redis and view it, you will see that your data is not In human-readable form, it is displayed as a byte array, similar to the following:
Of course, when data is obtained from Redis, the data will be converted as a byte array by default, which is determined according to the serialization strategy.
And stringredistemplate, the data stored by default is the original text, because stringRedistemplate uses the string serialization strategy by default, and the default data stored in stringredistemplate looks like this:
The reason for the difference between the two is because the serialization strategies used by the two are different during initialization. You can see the following by opening the source code:
public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V>, BeanClassLoaderAware { private boolean enableTransactionSupport = false; private boolean exposeConnection = false; private boolean initialized = false; private boolean enableDefaultSerializer = true; @Nullable private RedisSerializer<?> defaultSerializer; @Nullable private RedisSerializer keySerializer = null; @Nullable private RedisSerializer valueSerializer = null; @Nullable private RedisSerializer hashKeySerializer = null; @Nullable private RedisSerializer hashValueSerializer = null; // Other fields are slightly... public RedisTemplate() { } // This method is a method of overriding RedisAccessor RedisAccessor implements spring's InitializingBean, that is, this method is executed at startup. You can see that the default serialization of this method is JdkSerializationRedisSerializer public void afterPropertiesSet() { super.afterPropertiesSet(); boolean defaultUsed = false; if (this.defaultSerializer == null) { this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader()); } if (this.enableDefaultSerializer) { if (this.keySerializer == null) { this.keySerializer = this.defaultSerializer; defaultUsed = true; } if (this.valueSerializer == null) { this.valueSerializer = this.defaultSerializer; defaultUsed = true; } if (this.hashKeySerializer == null) { this.hashKeySerializer = this.defaultSerializer; defaultUsed = true; } if (this.hashValueSerializer == null) { this.hashValueSerializer = this.defaultSerializer; defaultUsed = true; } } if (this.enableDefaultSerializer && defaultUsed) { Assert.notNull(this.defaultSerializer, "default serializer null and not all serializers initialized"); } if (this.scriptExecutor == null) { this.scriptExecutor = new DefaultScriptExecutor(this); } this.initialized = true; } // The rest of the methods are... }
It can be seen that redistemplate is constructed without parameters during initialization. The serialization setting is completed by executing afterPropertiesSet at project startup through spring's bean loading mechanism. If you need to customize the serialization configuration, you can write a redistemplate bean yourself to complete the configuration.
stringredistemplate is relatively simple. It directly inherits redistemplate. string serialization is used by default during initialization. The source code is as follows:
public class StringRedisTemplate extends RedisTemplate<String, String> { public StringRedisTemplate() { this.setKeySerializer(RedisSerializer.string()); this.setValueSerializer(RedisSerializer.string()); this.setHashKeySerializer(RedisSerializer.string()); this.setHashValueSerializer(RedisSerializer.string()); } // Other methods are slightly... }
Then it can be concluded that if you want to use the default configuration to operate redis, if the data to be operated is a byte array, use redistemplate, and if the data to be operated is plaintext, use stringredistemplate.
Of course, when it is actually used in the project, the bean instance of redistemplate is generally customized to set a specific serialization strategy. To put it bluntly, redistemplate can realize the same serialization as stringredistemplate through custom bean, and it is more flexible to use.