Spring-data-redis:serializer instances

Source: Internet
Author: User
Tags object object

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
  1. <bean id="jedistemplate" class="Org.springframework.data.redis.core.RedisTemplate" >
  2. <property name="connectionfactory" ref="jedisconnectionfactory" ></property>
  3. <property name="Keyserializer" >
  4. <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
  5. </property>
  6. <property name="Hashkeyserializer" >
  7. <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
  8. </property>
  9. <property name="ValueSerializer" >
  10. <bean class="Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
  11. </property>
  12. <property name="Hashvalueserializer" >
  13. <bean class="Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
  14. </property>
  15. </bean>

2) Program Examples:

Java code
    1. valueoperations<string, user> valueoper = Redistemplate.opsforvalue ();
    2. User user = new User ("Zhangsan",12);
    3. Valueoper.set ("user:1", user);
    4. 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
  1. <bean id="Jsonserializer" class="Com.sample.redis.sdr.JsonRedisSerializer"/>
  2. <bean id="jedistemplate" class="Org.springframework.data.redis.core.RedisTemplate" >
  3. <property name="connectionfactory" ref="jedisconnectionfactory" ></property>
  4. <property name="Defaultserializer" >
  5. <bean class="Org.springframework.data.redis.serializer.StringRedisSerializer"/>
  6. </property>
  7. </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
  1. /**
  2. * Do not use the JSON serialization tool that comes with SDR, and all operations are based on string
  3. **/
  4. Public class jsonredisseriaziler{
  5. public static final String Empty_json = "{}";
  6. public static final Charset Default_charset = Charset.forname ("UTF-8");
  7. protected Objectmapper objectmapper = new Objectmapper ();
  8. Public Jsonredisseriaziler () {}
  9. /** 
  10. * Java-object as Json-string
  11. * @param Object
  12. * @return
  13. */
  14. Public String seriazileasstring (Object object) {
  15. if (object== null) {
  16. return Empty_json;
  17. }
  18. try {
  19. return this.objectMapper.writeValueAsString (object);
  20. } catch (Exception ex) {
  21. throw New SerializationException ("Could not write JSON:" + ex.getmessage (), ex);
  22. }
  23. }
  24. /** 
  25. * Json-string to Java-object
  26. * @param str
  27. * @return
  28. */
  29. Public <T> T deserializeasobject (String str,class<t> clazz) {
  30. if (str = = Null | | clazz = = null) {
  31. return null;
  32. }
  33. try{
  34. return this.objectMapper.readValue (str, clazz);
  35. }catch (Exception ex) {
  36. throw New SerializationException ("Could not write JSON:" + ex.getmessage (), ex);
  37. }
  38. }
  39. }
Java code
  1. Public class Redisclienttest {
  2. private Jsonredisseriaziler Seriaziler;
  3. private Redistemplate redistemplate;
  4. public void Setseriaziler (Jsonredisseriaziler seriaziler) {
  5. This.seriaziler = Seriaziler;
  6. }
  7. public void Setredistemplate (Redistemplate redistemplate) {
  8. this.redistemplate = redistemplate;
  9. }
  10. public void Insertuser (user user) {
  11. Valueoperations<string, string> operations = Redistemplate.opsforvalue ();
  12. Operations.set ("User:" + user.getname (), seriaziler.seriazileasstring (user));
  13. }
  14. Public User GetUser (String name) {
  15. Valueoperations<string, string> operations = Redistemplate.opsforvalue ();
  16. String json = operations.get ("User:" + name);
  17. return Seriaziler.deserializeasobject (JSON, User.   Class);
  18. }
  19. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.