In-depth understanding of the use of Spring Redis (II), RedisTemplate transaction support, serialization, redisredistemplate

Source: Internet
Author: User

In-depth understanding of the use of Spring Redis (II), RedisTemplate transaction support, serialization, redisredistemplate

In the previous article, we explained the basic usage of RedisTemplate, obtained the connection through RedisCallback, and then operated Redis. Most of the online tutorials are similar.

This is similar to the executeWithNativeSession method provided in HibernateTemplate. It is a synchronous callback mechanism in Java. Before and after the method, the system opens and closes the connection and sets the transaction for us.

 

RedisTemplate api details

1. RedisTemplate transactions
    private boolean enableTransactionSupport = false;    private boolean exposeConnection = false;    private boolean initialized = false;    private boolean enableDefaultSerializer = true;    private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();    private RedisSerializer keySerializer = null;    private RedisSerializer valueSerializer = null;    private RedisSerializer hashKeySerializer = null;    private RedisSerializer hashValueSerializer = null;    private RedisSerializer<String> stringSerializer = new StringRedisSerializer();    private ScriptExecutor<K> scriptExecutor;    // cache singleton objects (where possible)    private ValueOperations<K, V> valueOps;    private ListOperations<K, V> listOps;    private SetOperations<K, V> setOps;    private ZSetOperations<K, V> zSetOps;

EnableTransactionSupport: Whether to enable transaction support. We can find the place where this variable is used in the code. We can see that before calling RedisCallback, a line of code is conn = RedisConnectionUtils If transaction support is enabled. bindConnection (factory, enableTransactionSupport), that is, the system automatically helps us get the connection bound to the transaction. You can add, delete, and query Redis multiple times in one method, and always use the same connection. However, the producer uses the same connection and cannot enable transactions without going through connection.multi()on.exe c.

I did not carefully check the code, but I can know that Spring has already provided us with better support:@ Transactional

Add this annotation to the place where the execute () method in RedisTempalte is called (provided below the spring package, do not reference the annotation under the rt package), so that all execute in this method can be made, automatically add multi (), Roll Back abnormally, or submit during normal operation!

 

2. Serializer of RedisTempalte

Anyone who has used the jedis operation knows that all connection operation methods are passed in byte arrays. Then, to convert an object and byte to each other, it needs to be serialized and deserialized.

In the template method, Spring provides the default StringSerializer and JdkSerializer. The first is implemented by String. getBytes. In Redis, all stored values are of the string type. Therefore, after saving this method, you can clearly see what key and value we saved on the Redis-cli console. But for JdkSerializationRedisSerializer, this serialization method is provided by Jdk. First, we need to inherit the serialized class from the Serializeable interface, and then save it through the Jdk Object serialization method. (Note: This serialized object, even a String type, cannot be seen in the redis console because it stores additional information about the object type or something ,)

 

Such a long string is actually 123 of the int type.

 

KeySerializer: the default serializer for the key. The default value is StringSerializer.

ValueSerializer: This is the default serializer for value. The default value is JdkSerializationRedisSerializer from defaseriserializer.

HashKeySerializer: The hashkey serializer for hash data. The default value is JdkSerializationRedisSerializer from defaseriserializer.

HashValueSerializer: hashvalue serializer for hash data. The default value is JdkSerializationRedisSerializer from defaseriserializer.

 

In addition, we also found operation classes such as valueOps and hashOps in this class, which is a class provided by spring for us to operate Redis directly and is very convenient. In the next article, we will explain these classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.