Jedis-缓存操作具体实现代码。


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 redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by  YeCongZhi on 2017/6/21.
 */
@Configuration
public class JedisConfiguation {
    Logger logger = LoggerFactory.getLogger(JedisConfiguation.class);

    @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;

    @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;

    @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("JedisPoolA")
    public JedisPool redisPoolFactoryA() {
//        logger.info("[redisA服务地址:]" + hostA);
//        logger.info("[redisA服务端口:]" + portA);
//        logger.info("[redisA密码:]" + passwordA);
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMinIdle(minIdle);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        JedisPool jedisPool = new JedisPool(poolCofig, hostA, portA, 0, passwordA);
        return jedisPool;
    }

    @Bean("JedisPoolB")
    public JedisPool redisPoolFactoryB() {
//        logger.info("[redisB服务地址:]" + hostB);
//        logger.info("[redisB服务端口:]" + portB);
//        logger.info("[redisB密码:]" + passwordB);
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMinIdle(minIdle);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        JedisPool jedisPool = new JedisPool(poolCofig, hostB, portB, 0, passwordB);
        return jedisPool;
    }

}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by  YeCongZhi on 2017/6/21.
 * 使用示例
     @Autowired
    private JedisClientService jedisClientService;

     jedisClientService.setValueToA("AAAAA","TFDSFSDF333");
     System.out.println(jedisClientService.getValueFromA("AAAAA"));

     jedisClientService.setValueToB("BBBBB","AAREGRTE33");
     System.out.println(jedisClientService.getValueFromB("BBBBB"));
 */
@Service
public class JedisClientService {

    @Autowired
    @Qualifier("JedisPoolA")
    private JedisPool jedisPoolA;

    @Autowired
    @Qualifier("JedisPoolB")
    private JedisPool jedisPoolB;

    /**获取key的value值**/
    public synchronized String getValueFromA(String key) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolA.getResource();
        String str = "";
        try {
            str = jedis.get(key);
        } finally {
            try {
                /**close方法 包含两个方法
                 * returnBrokenResource
                 * returnResource*/
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**保存key - velue */
    public synchronized String setValueToA(String key, String value) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolA.getResource();
        String str = "";
        try {
            str = jedis.set(key, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**保存key - velue */
    public synchronized String setexValueToA(String key, int seconds, String value) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolA.getResource();
        String str = "";
        try {
            str = jedis.setex(key, seconds, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**删除key**/
    public synchronized Long delFromA(String key) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolA.getResource();
        Long cnt;
        try {
            cnt = jedis.del(key);
        } finally {
            try {
                /**close方法 包含两个方法
                 * returnBrokenResource
                 * returnResource*/
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cnt;
    }
    /******************************************************************/
    /**获取key的value值**/
    public synchronized String getValueFromB(String key) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolB.getResource();
        String str = "";
        try {
            str = jedis.get(key);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**保存key - velue */
    public synchronized String setValueToB(String key, String value) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolB.getResource();
        String str = "";
        try {
            str = jedis.set(key, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**保存key - velue */
    public synchronized String setexValueToB(String key, int seconds, String value) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolB.getResource();
        String str = "";
        try {
            str = jedis.setex(key, seconds, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
    /**删除key**/
    public synchronized Long delFromB(String key) {
        /**获取jedis实例*/
        Jedis jedis = jedisPoolB.getResource();
        Long cnt;
        try {
            cnt = jedis.del(key);
        } finally {
            try {
                /**close方法 包含两个方法
                 * returnBrokenResource
                 * returnResource*/
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cnt;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值