Before introducing spring Redis for storage, it was through the Defaultserializer in Redistemplate, the Jdkserializationredisserializer. The serialization of the JDK is relatively straightforward, but sometimes it can be viewed through the console at the time of online debugging, completely invisible. And in space occupancy and performance, compared to Jackson, there is absolutely no advantage.
There have been two times the problem on the line, the location of the time to know that the cache is wrong, but do not know exactly in that cache field, debugging is very inconvenient. So the serialization was replaced by Jackson.
The code is as follows:
ImportJava.lang.reflect.ParameterizedType;ImportJava.util.concurrent.TimeUnit;ImportJavax.annotation.Resource;ImportOrg.springframework.beans.factory.InitializingBean;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.HashOperations;Importorg.springframework.data.redis.core.ListOperations;ImportOrg.springframework.data.redis.core.RedisCallback;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.SetOperations;Importorg.springframework.data.redis.core.ValueOperations;ImportOrg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;ImportOrg.springframework.data.redis.serializer.RedisSerializer;/*** key is unified as String, omitted. HK is a hashkey type of hash type, HV is a value type or HashValue type (these two cannot exist at the same time, so take only one) *@authorHan*/ Public classBASEREDISDAO<HK, hv>Implementsinitializingbean{//class start for actual parameters PrivateClassHkclass; PrivateClassHvclass; PrivateClassGethkclass () {if(Hkclass = =NULL) {Hkclass= (class]; } returnHkclass; } PrivateClassGethvclass () {if(Hvclass = =NULL) {Hvclass= (class]; } returnHvclass; } //End@AutowiredPrivateRedistemplate<string, hv>redistemplate; protectedValueoperations<string, hv>valueoperations; // protectedHashoperations<string, HK, hv>hashoperations; // protectedListoperations<string, hv>listoperations; protectedSetoperations<string, hv>setoperations; /** * * @paramKey *@paramValue *@paramexpire *@return */ protected voidSet (String key, HV value,Longexpire) {Valueoperations.set (key, value, expire, timeunit.seconds); } /*** Get Value * *@paramKey *@return */ protectedHV get (String key) {returnValueoperations.get (key); } /*** Key Delete *@paramKey*/ protected voidDelete (String key) {getredistemplate (). Delete (key); } /*** Key exist *@paramKey *@return */ protected BooleanHaskey (String key) {returngetredistemplate (). Haskey (key); } /***key expire *@paramKey *@paramTimeout *@paramUnit *@return */ protectedBoolean expire (String key,Longtimeout,timeunit Unit) { returngetredistemplate (). Expire (key, timeout, unit); } /*** Redistemplate is globally unique, the subclass does not appear on the Redistemplate member variable settings (such as Keyserializer,) *@return */redistemplate<string, hv>getredistemplate () {returnredistemplate; } /*** When you need to change the serializer, you can do it directly through the Connection.set method *@paramCallback *@return */ protected<T> T Execute (rediscallback<t>callback) { returnRedistemplate.execute (callback); } /*** Get Stringserializer*/ protectedRedisserializer<string>Getstringserializer () {returnRedistemplate.getstringserializer (); } /*** Get Jdkserializationredisserializer*/@SuppressWarnings ("Unchecked") protected<T> redisserializer<t>Getdefaultserializer () {return(redisserializer<t>) Redistemplate.getdefaultserializer (); } /*** Get Stringserializer *@return */@SuppressWarnings ("Unchecked") protectedRedisserializer<string>Getkeyserializer () {return(redisserializer<string>) Redistemplate.getkeyserializer (); } /*** Get Jackson2jsonredisserializer *@return */ protectedRedisserializerGetvalueserializer () {return(redisserializer) Redistemplate.getvalueserializer (); } /*** Get Jackson2jsonredisserializer *@return */@SuppressWarnings ("Unchecked") protectedRedisserializerGethashkeyserializer () {return(redisserializer) Redistemplate.gethashkeyserializer (); } /*** Get Jackson2jsonredisserializer *@return */@SuppressWarnings ("Unchecked") protectedRedisserializerGethashvalueserializer () {return(redisserializer) Redistemplate.gethashvalueserializer (); } @Override Public voidAfterpropertiesset ()throwsException {if(Gethkclass () = =NULL|| Gethvclass () = =NULL){ Throw NewIllegalArgumentException ("Get generic class Failed"); } //Valueoperations =Redistemplate.opsforvalue (); Hashoperations=Redistemplate.opsforhash (); Listoperations=redistemplate.opsforlist (); Setoperations=Redistemplate.opsforset (); //Redistemplate.setvalueserializer (NewJackson2jsonredisserializer(Gethvclass ())); Redistemplate.sethashkeyserializer (NewJackson2jsonredisserializer(Gethkclass ())); Redistemplate.sethashvalueserializer (NewJackson2jsonredisserializer(Gethvclass ())); }}
For the serialization of key, directly in the configuration file definition
<BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate"Scope= "Prototype"> < Propertyname= "ConnectionFactory"ref= "Jedisconnectionfactory" /> < Propertyname= "Keyserializer"ref= "Stringkeyserializer"/> < Propertyname= "Enabletransactionsupport"value= "true"/><!--Configure true to use transactional to control transactions, spring has provided support - </Bean>
Deep understanding of the use of spring Redis (vii), Spring Redis using Jackson serialization, and Basedao code