The previous article said redistemplate support for annotation transactions, as well as the provided serializer.
The transaction needs to open Enabletransactionsupport, and then use @transactional annotations, which directly through the callback connection, do not need to do their own multi and exec transaction open commit. But the callback to get connection, completely did not achieve the function of a template class. So in this article we will talk about the methods provided by several operations interfaces.
Private Valueoperations<k, v> valueops; Private Listoperations<k, v> listops; Private Setoperations<k, v> setops; Private Zsetoperations<k, v> Zsetops;
1. Redisoperations Interface Description
The implementation class of this interface is the redistemplate itself, mainly provides some support for Redis key, transaction, run script and other commands, not responsible for data reading and writing.
Let's choose a piece of code to look at:
Public void Watch (K key) { finalbyte[] Rawkey = rawkey (key); Execute (new rediscallback<object>() { public Object Doinredis ( Redisconnection connection) { connection.watch (rawkey); return NULL ; } true ); }
This is a monitoring command of the transaction, it can be seen that the implementation is also used in the callback rediscallback. These APIs are encapsulated through the connection API.
2. Valueoperations Interface Description
The implementation class for this interface inherits Abstractoperation for the Defaultvalueoperations,default class, so let's look at the constructor for this class:
Defaultvalueoperations (redistemplate<k, v> template) { super(template); }
NonPublic, it needs to be constructed by passing in a template. But we are not able to access it. But don't worry, in Redistemplate, a factory method has been provided: Opsforvalue (). This method returns a default action class. In addition, we can inject directly through the annotation @resource (name = "Redistemplate").
@Resource (name = "Redistemplate") private valueoperations<string, object> vops;
Isn't it strange that two classes without a father-son relationship can be injected into each other? This is the spring editor mechanism, search the next valueoperationseditor this class to know, spring in the injection of time called the editor's SetValue method. Understand the spring source code should know. I also read other people's articles to understand. Article address
In fact, in addition to the template can be injected into the valueoperations, you can also inject the above several other operations and hashoperations.
Defaultvalueoperations provides an operational API for all Redis string types. Like SET,GET,INCR and so on. Using these methods, you can easily store arbitrary Java types directly without having to serialize and deserialize the stored things yourself.
Note: The types of generics, as well as the serializer, are from redistemplate. If you want to do JSON or other serialization yourself, you need to make changes in redistemplate.
In addition, for the member Listops,setops,zsetops this next operation class, will not repeat, the principle with valueoperations, is other several kinds of data type realization.
3. Hashoperations Interface Description
This interface does not define a member variable, guessing that because of the addition of additional keys and value to the hash, generics cannot be defined in front of the member, so the method is provided directly. Fortunately, the Jdkserializer is used, so this definition of what type of generics does not produce a running exception.
Public voidput (K key, HK HashKey, HV value) {Final byte[] Rawkey =Rawkey (key); Final byte[] Rawhashkey =Rawhashkey (HashKey); Final byte[] Rawhashvalue =Rawhashvalue (value); Execute (NewRediscallback<object>() { PublicObject Doinredis (redisconnection connection) {Connection.hset (Rawkey, Rawhashkey, Rawhashvalue); return NULL; } }, true); }
This is the basic put method, you can see the key and HashKey two, called the different serialization methods. In the method view, see serialization HashKey with the template in the Hashkeyserializer, the default is Jdkserializer, this will lead to the Redis console, can not see what key is saved. If you don't like it, you can set it up by Redistemplate.sethashkeyserializer ().
All the APIs for the hash structure are encapsulated in this class, for example, Hset becomes put,hget to get. You need to see the API yourself. Don't be misled.
4. Summary
So far, the basic use has been finished. Said is very rough, but I feel prone to misunderstanding points of emphasis, the basic operation of the need to look at their own a little. Right to make a contribution to it. Next, you might summarize some of the issues that you encounter with Redis over the next two months.
I put the code on GIT, the address is Https://github.com/55375829/learn, inside Spjedis is the project that was built when I was learning.
Deep understanding of the use of Spring Redis (iii), access to Redis using the Redistemplate operation class