spring-boot-starter-redis 集成

redis支持

代码块

<!-- 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>

更多支持

http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis

redis配置

# 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代码

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
 **/
@Component
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 = redisConnectionFactory.getConnection();
        try {
            connection.setEx(key.getBytes(), expire, value.getBytes());
        }catch (Exception e){
            logger.warn("redis 保存数据出现情况,现发起一次重复连接", 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.getConnection();
        try {
            connection.del(key.getBytes());
        } finally {
            if (connection != null)
                connection.close();
        }
    }

    public String get(String key) throws Exception{
        RedisConnection connection = redisConnectionFactory.getConnection();
        byte[] bytes = null;
        try {
            bytes = connection.get(key.getBytes());
        }finally {
            if (connection != null)
                connection.close();
        }
        if (bytes == null) {
            logger.warn("redis缓存无此key,可能是超时失效了");
            throw new Exception("redis 失效");
        }
        return  new String(bytes);
    }

    public void set(String key,String value) throws Exception{
        RedisConnection connection = redisConnectionFactory.getConnection();
        try {
            connection.set(key.getBytes(), value.getBytes());
        }finally {
            if (connection != null)
                connection.close();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值