The use of several commonly used interface methods is provided in Redistemplate, respectively:
Private valueoperations<k, v> valueops;
Private listoperations<k, v> listops;
Private setoperations<k, v> setops;
Private zsetoperations<k, v> zsetops;
1 2 3 4
This article mainly explains the use of several interfaces. Redisoperations
The implementation class for this interface is redistemplate, providing some action on the Redis command. Valueoperations
The implementation class for this interface is: Defaultvalueoperations.
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").
Declaration
@Resource (name = "Redistemplate")
private redistemplate<string, string> template;
Call Method
Template.opsforvalue (). Set ("Key", "value");
1 2 3 4 5 6
The redistemplate also provides a corresponding *operationseditor, which is used to directly inject the corresponding operation through the redistemplate.
//Declaration
@Resource (name = "Redistemplate")
private valueoperations<string, object> vops;
Call method
Vops.set ("Key", "value");
1 2 3 4 5 6 7
In addition to being able to inject valueoperations through template, you can inject several other operations and hashoperations
Defaultvalueoperations provides an operation API for all Redis string types. Like SET,GET,INCR and so on. Using these methods, you can easily store any Java type directly without having to serialize and deserialize the stored object.
Listoperations,setoperations,zsetoperations In addition to the operational APIs provided, other methods of invocation are consistent with defaultvalueoperations. Hashoperations Interface Description
This interface does not define a member variable, but provides a method directly.
Public <HK, Hv> hashoperations<k, HK, hv> Opsforhash () {return
new defaulthashoperations<k, HK, Hv> ;(this);
}
1 2 3
The specific call is as follows:
Method 1
Inject the Hashoperations object
@Resource (name = "Redistemplate")
private hashoperations<string,string,object> Hashops;
Specific call
map<string,string> Map = new hashmap<string, string> ();
Map.put ("Value", "code");
Map.put ("Key", "KeyValue");
Hashops.putall ("Hashops", map);
1 2 3 4 5 6 7 8 9
Method 2
Inject the Redistemplate object
@Resource (name = "Redistemplate")
private redistemplate<string, string> template;
Specific call
map<string,string> Map = new hashmap<string, string> ();
Map.put ("Value", "code");
Map.put ("Key", "KeyValue");
Template.opsforhash (). Putall ("Hashops", map);
1 2 3 4 5 6 7 8 9 [java] View plain copy stringredistemplate.opsforvalue (). Set ("Test", "M", 60*10,ti Meunit.seconds)//To Redis data and set cache time [java] View plain copy Stringredistemplate.boundvalueops ("Test"). Increment ( -1);//val-1 Operations [java] View Plain copy Stringredistemplate.opsforvalue (). Get ("test")//val [java] view Plain in cache based on key copy Stringredistemplate.boundvalueops ("Test"). Increment (1);//val +1 [java] View Plain Copy stringredistemplate.getexpire ("Test")//Gets the expiration time according to key [java] View Plain copy Stringredistemplate.getexpire ("Test", timeunit.seconds)//Gets the expiration time according to key and translates to the specified unit [java] view Plain copy stringredistemplate.delete ("test")//delete cache [java] view plain by key copy Stringredistemplate.haskey ("546545")//check key exists, return Boolean value [java] view Plain copy Stringredistemplate.opsforset (). ADD ("red_123", "1", "2", "3");//To store set set [java] view Plain in specified key copy Stringredistemplate.expire ("red_123", 1000 , timeunit.milliseconds)//Set Expiration time [java] View Plain copy Stringredistemplate.opsforset (). IsMember ("red_123", "1")//depending on key to see if the specified data exists in the collection [java] View plain copy stringredistemplate.opsforset (). Members ("red_123")//Get Set set by key [java] View Plain copy