Spring Boot Tutorial (34) using the Redis database (2)

Source: Internet
Author: User
Tags assert object object spring boot tutorial

In addition to string types, we often store objects in Redis, and then we wonder if we can RedisTemplate<String, User> initialize and manipulate them using similar. However, spring boot does not support direct use, we need to implement the RedisSerializer<T> interface to serialize and deserialize incoming objects, and we will perform the read and write operations of the object through an instance.

  • To create an object to store: User
    public class User implements Serializable {    private static final long serialversionuid = -1l;    Private String username;    Private Integer age;    Public User (String username, Integer age) {        this.username = Username;        This.age = age;    }    Omit getter and Setter}

      

  • The
  • implements the serialization interface for the object
    public class Redisobjectserializer implements Redisserializer  <Object> {private Converter<object, byte[]> serializer = new Serializingconverter ();  Private converter<byte[], object> deserializer = new Deserializingconverter ();  Static final byte[] Empty_array = new Byte[0];    Public Object deserialize (byte[] bytes) {if (IsEmpty (bytes)) {return null;    } try {return Deserializer.convert (bytes);    } catch (Exception ex) {throw new SerializationException ("Cannot deserialize", ex);    }} public byte[] Serialize (Object object) {if (object = = null) {return empty_array;    } try {return Serializer.convert (object);    } catch (Exception ex) {return empty_array;  }} Private Boolean IsEmpty (byte[] data) {return (data = = NULL | | data.length = = 0); }} 

  • Configure the Redistemplate instance for user
     @Configurationpublic class Redisconfig {@Bean Jed    Isconnectionfactory jedisconnectionfactory () {return new jedisconnectionfactory (); } @Bean public redistemplate<string, user> redistemplate (Redisconnectionfactory factory) {Redistemplat        e<string, user> template = new redistemplate<string, user> ();        Template.setconnectionfactory (Jedisconnectionfactory ());        Template.setkeyserializer (New Stringredisserializer ());        Template.setvalueserializer (New Redisobjectserializer ());    return template; }} 

    &NBSP;  
  • After the configuration is completed, write the test case experiment effect
    @RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (application.class) public class applicationtests {@Autowiredprivate redistemplate<string, user> redistemplate; @Testpublic void Test () throws Exception {//Save object User user = New User ("Superman"); Redistemplate.opsforvalue (). Set (User.getusername (), user); user = new use R ("Batman"), Redistemplate.opsforvalue (). Set (User.getusername (), user), user = new User ("Spider-Man", 40); Redistemplate.opsforvalue (). Set (User.getusername (), user); Assert.assertequals (Redistemplate.opsforvalue (). Get ("Superman"). Getage (). Longvalue ()); Assert.assertequals (Redistemplate.opsforvalue (). Get ("Batman"). Getage (). Longvalue ()); Assert.assertequals (Redistemplate.opsforvalue (). Get ("Spider-Man"). Getage (). Longvalue ());}}

      

    Of course the data operations provided in Spring-data-redis are much more than this, this article is only used as a configuration reference when using Redis in spring boot, and more for redis operations, refer to Spring-data-redis Reference.

  • Source Source

Spring Boot Tutorial (34) using the Redis database (2)

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.