In-depth understanding of the use of Spring Redis (ii), Redistemplate transaction support, serialization

Source: Internet
Author: User
Tags object serialization

In the previous article we explained the basic use of redistemplate, through Rediscallback to obtain connection, and then to operate Redis. Most of the tutorials on the web are also done in this way.

This executewithnativesession method, similar to the one provided in Hibernatetemplate, is a synchronous callback mechanism in Java. Before and after the method, the system opens and closes the connection, sets up the transaction, and so on.

Redistemplate API Detailed

1. Redistemplate's Business
    Private BooleanEnabletransactionsupport =false; Private BooleanExposeconnection =false; Private Booleaninitialized =false; Private BooleanEnabledefaultserializer =true; PrivateRedisserializer<?> Defaultserializer =NewJdkserializationredisserializer (); PrivateRedisserializer Keyserializer =NULL; PrivateRedisserializer ValueSerializer =NULL; PrivateRedisserializer Hashkeyserializer =NULL; PrivateRedisserializer Hashvalueserializer =NULL; PrivateRedisserializer<string> Stringserializer =NewStringredisserializer (); PrivateScriptexecutor<k>Scriptexecutor; //Cache Singleton objects (where possible)    PrivateValueoperations<k, v>Valueops; PrivateListoperations<k, v>Listops; PrivateSetoperations<k, v>Setops; PrivateZsetoperations<k, v> Zsetops;

enabletransactionsupport: Whether transaction support is enabled. In the code where we search for this variable, we see that before calling Rediscallback, there is a line of code that if transaction support is enabled, then conn = Redisconnectionutils.bindconnection (Factory, Enabletransactionsupport), that is, the system automatically helps us get the connection that is bound in the transaction. Redis additions and deletions can be made multiple times in a single method, always using the same connection. However, the transaction cannot be enabled even if the same connection is used, without Connection.multi () and Connection.exec ().

I didn't look through the code carefully, but I can tell that spring has given us a better support for this:@Transactional

In the call to the Execute () method in Redistempalte, add this annotation (which is provided below the spring package, do not refer to the annotations under the RT package), allowing all execute in this method to automatically join multi () As well as exception rollback or a normal runtime commit!

2. Redistempalte's Serializer

All Jedis operations know that all connection methods are incoming byte arrays. Then, converting an object and byte to each other requires serialization and deserialization.

In the template method, Spring provides the default Stringserializer and Jdkserializer, the first one is simple, which is achieved by string.getbytes (). And in Redis, all stored values are of type string. So after this method is saved, through the REDIS-CLI console, it is possible to clearly see what key,value is that we have saved. But for Jdkserializationredisserializer, this serialization method is provided by the JDK. First, we want the class to be serialized to inherit from the Serializeable interface, which is then passed and then saved through the JDK object serialization method. (Note: This serialized saved object, even if it is a string type, is not visible in the Redis console, because it holds additional information about what type of object it is,)

Such a long string, is actually an int type of 123.

Keyserializer: This is the default serializer for key. The default value is Stringserializer.

ValueSerializer: This is the default serializer for value, and the default value is the Jdkserializationredisserializer taken from Defaultserializer.

Hashkeyserializer: HashKey Serializer for hash structure data, the default value is Jdkserializationredisserializer from Defaultserializer.

Hashvalueserializer: HashValue Serializer for hash structure data, the default value is Jdkserializationredisserializer from Defaultserializer.

In addition, we found in this class operations such as Valueops and Hashops, which is very handy for the classes that spring gives us to use directly to manipulate Redis. We will explain these classes in the next article.

In-depth understanding of the use of Spring Redis (ii), Redistemplate transaction support, serialization

Related Article

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.