Spring-data-redis offers a variety of serializer strategies that are very handy for developers using Jedis. SDR offers 4 built-in serializer:
- Jdkserializationredisserializer: Using the JDK serialization means (serializable interface, Objectinputstrean,objectoutputstream), the data is stored in a byte stream
- Stringredisserializer: String encoding, data is stored as String
- Jacksonjsonredisserializer:json Format Storage
- Oxmserializer:xml Format Storage
Among them, Jdkserializationredisserializer and Stringredisserializer are the most basic serialization strategies, where "Jacksonjsonredisserializer" and " Oxmserializer "are all based on stirng storage, so they are more" advanced "serialization (eventually using string parsing and building Java objects).
The redistemplate needs to declare 4 kinds of serializer, the default is "Jdkserializationredisserializer":
1) Keyserializer: For normal k-v operation, Key takes a serialization strategy
2) serialization strategy taken by Valueserializer:value
3) Hashkeyserializer: In the hash data structure, Hash-key's serialization strategy
4) serialization strategy for Hashvalueserializer:hash-value
in any case, Key/hashkey is recommended to use Stringredisserializer.
Next, the examples describe how to use them, and you can first refer to the Spring-data-redis feature:
I. Jdkserializationredisserializer/stringredisserializer
1) Spring configuration file
Java code
- <bean id="jedistemplate" class="Org.springframework.data.redis.core.RedisTemplate" >
- <property name="connectionfactory" ref="jedisconnectionfactory" ></property>
- <property name="Keyserializer" >
- <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="Hashkeyserializer" >
- <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="ValueSerializer" >
- <bean class="Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- <property name="Hashvalueserializer" >
- <bean class="Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- </bean>
2) Program Examples:
Java code
- valueoperations<string, user> valueoper = Redistemplate.opsforvalue ();
- User user = new User ("Zhangsan",12);
- Valueoper.set ("user:1", user);
- System.out.println (Valueoper.get ("user:1"). GetName ());
Where user is the Pojo class, and the serializable interface needs to be implemented.
Two. SDR and JSON
1) Spring Configuration:
Java code
- <bean id="Jsonserializer" class="Com.sample.redis.sdr.JsonRedisSerializer"/>
- <bean id="jedistemplate" class="Org.springframework.data.redis.core.RedisTemplate" >
- <property name="connectionfactory" ref="jedisconnectionfactory" ></property>
- <property name="Defaultserializer" >
- <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- </bean>
There is no use of Jacksonjsonredisserializer in the config file, because it is cumbersome and inflexible (mainly Jackson needs ClassType). We're going to convert in Java code, because using the Jackson tool to convert the JSON string to JavaBean is very straightforward with Java code.
2) Program Examples:
Java code
- /**
- * Do not use the JSON serialization tool that comes with SDR, and all operations are based on string
- **/
- Public class jsonredisseriaziler{
- public static final String Empty_json = "{}";
- public static final Charset Default_charset = Charset.forname ("UTF-8");
- protected Objectmapper objectmapper = new Objectmapper ();
- Public Jsonredisseriaziler () {}
- /**
- * Java-object as Json-string
- * @param Object
- * @return
- */
- Public String seriazileasstring (Object object) {
- if (object== null) {
- return Empty_json;
- }
- try {
- return this.objectMapper.writeValueAsString (object);
- } catch (Exception ex) {
- throw New SerializationException ("Could not write JSON:" + ex.getmessage (), ex);
- }
- }
- /**
- * Json-string to Java-object
- * @param str
- * @return
- */
- Public <T> T deserializeasobject (String str,class<t> clazz) {
- if (str = = Null | | clazz = = null) {
- return null;
- }
- try{
- return this.objectMapper.readValue (str, clazz);
- }catch (Exception ex) {
- throw New SerializationException ("Could not write JSON:" + ex.getmessage (), ex);
- }
- }
- }
Java code
- Public class Redisclienttest {
- private Jsonredisseriaziler Seriaziler;
- private Redistemplate redistemplate;
- public void Setseriaziler (Jsonredisseriaziler seriaziler) {
- This.seriaziler = Seriaziler;
- }
- public void Setredistemplate (Redistemplate redistemplate) {
- this.redistemplate = redistemplate;
- }
- public void Insertuser (user user) {
- Valueoperations<string, string> operations = Redistemplate.opsforvalue ();
- Operations.set ("User:" + user.getname (), seriaziler.seriazileasstring (user));
- }
- Public User GetUser (String name) {
- Valueoperations<string, string> operations = Redistemplate.opsforvalue ();
- String json = operations.get ("User:" + name);
- return Seriaziler.deserializeasobject (JSON, User. Class);
- }
- }
Three. SDR and XML
The implementation can be found in the article "SDR and JSON", as well as reference to SPRING-OXM related documents.
Spring-data-redis:serializer instances