Spring boot-Integrated Redis (vi)

Source: Internet
Author: User
Tags serialization redis desktop manager

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)

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.