Spring Boot (iii): Use of Redis in spring boot

Source: Internet
Author: User
Tags redis redis server

Spring boot encapsulates the NoSQL database with the usual database support. Redis Introduction

Redis is the most widely used memory data store in the industry today. Memcached,redis supports richer data structures, such as hashes, lists, sets, etc., while supporting data persistence. In addition, Redis also provides some characteristics of the class database, such as transaction, HA, master-slave library. It can be said that Redis has some characteristics of caching system and database, so it has rich application scene. This article describes two typical scenarios for Redis in spring boot. How to use

1. Introduction of Spring-boot-starter-redis

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId> Spring-boot-starter-redis</artifactid>  
</dependency>  

2. Add configuration file

# Redis (redisproperties)
# Redis Database index (default 0)
spring.redis.database=0  
# Redis server address
spring.redis.host=192.168.0.58
# Redis Server connection Port
spring.redis.port=6379  
# Redis Server connection password (default is null)
spring.redis.password=  
# Connection Pool Maximum connection number (using negative values to indicate no limit)
spring.redis.pool.max-active=8  
Connection pool Maximum blocking wait time (using negative values to indicate no limit)
spring.redis.pool.max-wait=-1  
# Maximum idle connection in a connection pool
spring.redis.pool.max-idle=8  
# Minimum idle connection in the pool
spring.redis.pool.min-idle=0  
# Connection Timeout (ms)
spring.redis.timeout=0  

3, add Cache configuration class

@Configuration @EnableCaching public class Redisconfig extends cachingconfigurersupport{@Bean public keygene  Rator Keygenerator () {return new Keygenerator () {@Override The public object generate (object
                Target, method method, Object ... params) {StringBuilder sb = new StringBuilder ();
                Sb.append (Target.getclass (). GetName ());
                Sb.append (Method.getname ());
                for (Object obj:params) {sb.append (obj.tostring ());
            return sb.tostring ();
    }
        };
        @SuppressWarnings ("Rawtypes") @Bean public CacheManager CacheManager (redistemplate redistemplate) {
        Rediscachemanager RCM = new Rediscachemanager (redistemplate);
    Set cache expiration//rcm.setdefaultexpiration (60);//sec return RCM; @Bean public redistemplate<string, string> redistemplate (Redisconnectionfactory factory) {stringredistemplate template = new Stringredistemplate (factory);
        Jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2jsonredisserializer (Object.class);
        Objectmapper om = new Objectmapper ();
        Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);
        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);
        Jackson2jsonredisserializer.setobjectmapper (OM);
        Template.setvalueserializer (Jackson2jsonredisserializer);
        Template.afterpropertiesset ();
    return template; }

}

3, OK, then you can directly use the

@RunWith (Springjunit4classrunner.class)
@SpringApplicationConfiguration (application.class) Public
class Testredis {

    @Autowired
    private stringredistemplate stringredistemplate;
    
    @Autowired
    private redistemplate redistemplate;

    @Test public
    void Test () throws Exception {
        stringredistemplate.opsforvalue (). Set ("AAA", "a");
        Assert.assertequals ("A", stringredistemplate).
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.