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)