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) { final byte[] 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 void put (K key, HK HashKey, HV value) { final byte[] Rawkey = Rawkey (key); Final byte[] Rawhashkey = Rawhashkey (HashKey); Final byte[] Rawhashvalue = rawhashvalue (value); Execute (new rediscallback<object> () {public Object 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.
This article transferred from: http://www.cnblogs.com/luochengqiuse/p/4641256.html
accessing Redis (GO) using the Redistemplate operation class