Redis-RedisTemplate 缓存操作具体实现代码


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by  YeCongZhi on 2017/6/15.
 * Redis 多台服务器 多库的配置
 * 使用示例:
 *    @Autowired
      @Qualifier("redisA")
      private RedisTemplate redisTemplateA;
     @Autowired
     @Qualifier("redisB")
     private RedisTemplate redisTemplateB;

     redisTemplateA.opsForValue().set("redisA","aaa"); //存数据from A
     System.out.println(redisTemplateA.opsForValue().get("redisA"));//取 数据from A

     redisTemplateB.opsForValue().set("redisB","bbb"); //存数据from B
     System.out.println(redisTemplateB.opsForValue().get("redisB"));//取数据from B
 */
@Configuration
public class RedisConfiguation {

    Logger logger = LoggerFactory.getLogger(getClass());

    /**RedisA*/
    @Value("${redisA.host:}")//IP
    private String hostA;
    @Value("${redisA.port:}")//端口
    private int portA;
    @Value("${redisA.password:}")//密码
    private String passwordA;
    @Value("${redisA.database:}")//数据库
    private int databaseA;
    /**RedisB*/
    @Value("${redisB.host:}")//IP
    private String hostB;
    @Value("${redisB.port:}")//端口
    private int portB;
    @Value("${redisB.password:}")//密码
    private String passwordB;
    @Value("${redisB.database:}")//数据库
    private int databaseB;
    /**RedisC*/
    @Value("${redisC.host:}")//IP
    private String hostC;
    @Value("${redisC.port:}")//端口
    private int portC;
    @Value("${redisC.password:}")//密码
    private String passwordC;
    @Value("${redisC.database:}")//数据库
    private int databaseC;

    @Value("${redis.maxTotal:}")//最大连接数
    private int maxTotal;
    @Value("${redis.maxIdle:}")//最大空闲连接数
    private int maxIdle;
    @Value("${redis.minIdle:}")//最小空闲连接数
    private int minIdle;
    @Value("${redis.maxWaitMillis:}")//获取连接时的最大等待毫秒数
    private Long maxWaitMillis;
    @Value("${redis.testOnBorrow:false}")//在获取连接的时候检查有效性
    private boolean testOnBorrow;
    @Value("${redis.MinEvictableIdleTimeMillis:}")//多长时间后回收空闲连接
    private Long MinEvictableIdleTimeMillis;

    @Bean(name = "redisA")
    public RedisTemplate<String, String> redisTemplateA() {
        logger.info("[redisA服务地址:]" + hostA);
        logger.info("[redisA服务端口:]" + portA);
        logger.info("[redisA密码:]" + passwordA);
        StringRedisTemplate temple = new StringRedisTemplate();
        RedisConnectionFactory connectionFactory =  connectionFactory(
                hostA, portA, passwordA, databaseA,
                maxTotal,maxIdle,maxIdle,maxWaitMillis,MinEvictableIdleTimeMillis,
                testOnBorrow);
        temple.setConnectionFactory(connectionFactory);
        return temple;
    }

    @Bean(name = "redisB")
    public RedisTemplate<String, String> redisTemplateB() {
        logger.info("[redisB服务地址:]" + hostB);
        logger.info("[redisB服务端口:]" + portB);
        logger.info("[redisB密码:]" + passwordB);
        StringRedisTemplate temple = new StringRedisTemplate();
        RedisConnectionFactory connectionFactory =  connectionFactory(
                hostB, portB, passwordB, databaseB,
                maxTotal,maxIdle,maxIdle,maxWaitMillis,MinEvictableIdleTimeMillis,
                testOnBorrow);
        temple.setConnectionFactory(connectionFactory);
        return temple;
    }
    @Bean(name = "redisC")
    public RedisTemplate<String, String> redisTemplateC() {
        logger.info("[redisC服务地址:]" + hostC);
        logger.info("[redisC服务端口:]" + portC);
        logger.info("[redisC密码:]" + passwordC);
        StringRedisTemplate temple = new StringRedisTemplate();
        RedisConnectionFactory connectionFactory =  connectionFactory(
                hostC, portC, passwordC, databaseC,
                maxTotal,maxIdle,maxIdle,maxWaitMillis,MinEvictableIdleTimeMillis,
                testOnBorrow);
        temple.setConnectionFactory(connectionFactory);
        return temple;
    }

    /**建立redis连接*/
    public RedisConnectionFactory connectionFactory(
            String hostName, int port,String password,int database,
            int maxTotal,int maxIdle,int minIdle,long maxWaitMillis,long MinEvictableIdleTimeMillis,
            boolean testOnBorrow) {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(hostName);
        jedis.setPort(port);
        if (!StringUtils.isEmpty(password)) {
            jedis.setPassword(password);
        }
        if (database != 0) {
            jedis.setDatabase(database);
        }
        JedisPoolConfig poolCofig = poolCofig(
                maxTotal,maxIdle,minIdle,
                maxWaitMillis,MinEvictableIdleTimeMillis,testOnBorrow);
        jedis.setPoolConfig(poolCofig);
        /**初始化连接pool*/
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;
        return factory;
    }

    /**redis配置*/
    public JedisPoolConfig poolCofig(
            int maxTotal,int maxIdle, int minIdle,
            long maxWaitMillis,long MinEvictableIdleTimeMillis, boolean testOnBorrow) {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMinIdle(minIdle);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        return poolCofig;
    }

}

版权声明:本文为u013623705原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。