Spring-boot-starter-redis Integration

Source: Internet
Author: User
Tags redis server
Redis Support

code block

<!--Https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis-->
< dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> spring-boot-starter-redis</artifactid>
    <version>1.0.1.RELEASE</version>
</ Dependency>

More support

Http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis Redis Configuration

# Redis (redisproperties)  
spring.redis.database= # database name  
spring.redis.host=localhost # Server Host  
spring.redis.password= # server password  
spring.redis.port=6379 # connection Port  
Spring.redis.pool.max-idle=8 # Pool Settings ...  
Spring.redis.pool.min-idle=0  
spring.redis.pool.max-active=8  
spring.redis.pool.max-wait=-1  
Spring.redis.sentinel.master= # Name of Redis server  
spring.redis.sentinel.nodes= # comma-separated List of host: Port pairs  
Java Code
Package com.pay.channel.business;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.redis.connection.RedisConnection;
Import Org.springframework.data.redis.connection.RedisConnectionFactory;

Import org.springframework.stereotype.Component; /** * Redis * com.pay.channel.business.RedisBusiness * * * @email h_anke@163.com * @create 2017-03-06 13:57 @Compo

    Nent public class Redisbusiness {private static final Logger Logger = Loggerfactory.getlogger (Redisbusiness.class);

    @Autowired private Redisconnectionfactory redisconnectionfactory; public void Setex (string key, string value, int expire) throws exception{redisconnection connection = Redisconne
        Ctionfactory.getconnection ();
        try {Connection.setex (key.getbytes (), expire, value.getbytes ());
        }catch (Exception e) {logger.warn ("Redis save data occurrence, now initiates a repeat connection", e);    if (connection!= null) connection.close ();
            Connection = Redisconnectionfactory.getconnection ();
        Connection.setex (Key.getbytes (), expire, value.getbytes ());
        }finally {if (connection!= null) connection.close (); } public void Del (String key) throws exception{redisconnection connection = Redisconnectionfactory.get
        Connection ();
        try {Connection.del (key.getbytes ());
        finally {if (connection!= null) connection.close (); } public String get (string key) throws exception{redisconnection connection = REDISCONNECTIONFACTORY.G
        Etconnection ();
        byte[] bytes = NULL;
        try {bytes = Connection.get (Key.getbytes ());
        }finally {if (connection!= null) connection.close (); } if (bytes = = null) {Logger.warn ("Redis cache does not have this key, may be timed outExpired ");
        throw new Exception ("Redis failure");
    Return to New String (bytes); public void Set (String key,string value) throws exception{redisconnection connection = redisconnectionfact
        Ory.getconnection ();
        try {connection.set (key.getbytes (), value.getbytes ());
        }finally {if (connection!= null) connection.close ();
 }
    }

}
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.