Redis Installation
Reference: https://www.cnblogs.com/LQBlog/p/9214517.html
Stand-alone version
1. Add Pom Dependency
<!--Spring Boot Reids dependent - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-data-redis</Artifactid> <version>1.5.7.RELEASE</version> </Dependency> <!--spring2.0 required common-pool2 for integrated Redis - <Dependency> <groupId>Org.apache.commons</groupId> <Artifactid>Commons-pool2</Artifactid> <version>2.4.2</version> </Dependency>
2.application.properties Configuring Redis Connection Information
Spring Redis: host:192.168.65.128 port:6379 timeout:0 Password: # password is not filled
3. Add test Code
@Service Public classAddressserviceimplextendsBaseserviceimpl<address>ImplementsAddressservice {@Resource addressmapper addressmapper; @AutowiredPrivateRedistemplate<object,object>redistemplate; @Override PublicAddress Selectbyprimarykey (Object key) {String Rediskey=key.tostring (); Address Address=(Address) Redistemplate.opsforvalue (). get (Rediskey); if (address==null) {//prevents the cache from penetrating synchronized (this) {address = (address) Redistempla Te.opsforvalue (). get (Rediskey); if (address = = NULL) {address = Super.selectbyprimarykey (key); Redistemplate.opsforvalue (). Set (Rediskey, address); } } }Else{System.out.println ("Use cached."); } returnaddress; }}
Red code is to prevent memory penetration, such as your business method has 10,000 people at the same time access, if not locked. When the first access to determine the address is empty and then all to check the database, normal should be a connection query database and set the cache after 9999 all use the cache
Viewing results with Redis Desktop Manager
Because both the default key and value are serialized in the JDK's own jdkserializationredisserializer can be found to be poorly readable
Customizing serialization methods
Added a Redisconfig configuration serialization method
@Configuration Public classRedisconfig {/*** redistemplate serialization using jdkserializeable, storing binary bytecode, so custom serialization class *@paramRedisconnectionfactory *@return */@Bean PublicRedistemplate<object, object>redistemplate (redisconnectionfactory redisconnectionfactory) {redistemplate<object, object> redistemplate =NewRedistemplate<>(); Redistemplate.setconnectionfactory (redisconnectionfactory); //Replace default serialization with Jackson2jsonredisserializeJackson2jsonredisserializer Jackson2jsonredisserializer =NewJackson2jsonredisserializer (Object.class); Objectmapper Objectmapper=NewObjectmapper (); Objectmapper.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY); Objectmapper.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL); Jackson2jsonredisserializer.setobjectmapper (Objectmapper); //set serialization rules for value and serialization rules for keyRedistemplate.setkeyserializer (NewStringredisserializer ()); Redistemplate.setvalueserializer (Jackson2jsonredisserializer); Redistemplate.afterpropertiesset (); returnredistemplate; }}
Test results again
Spring boot-Integrated Redis (vi)