redis知识

如有错误希望指出,谢谢

一、redis 池创建

public class RedisPool {

    private static JedisPool pool;//jedis连接池

    private static int maxTotal = 20;//最大连接数

    private static int maxIdle = 10;//最大空闲连接数

    private static int minIdle = 5;//最小空闲连接数

    private static boolean testOnBorrow = true;//在取连接时测试连接的可用性

    private static boolean testOnReturn = false;//再还连接时不测试连接的可用性

    static {
        initPool();//初始化连接池
    }

    public static Jedis getJedis(){
        return pool.getResource();
    }

    public static void close(Jedis jedis){
        jedis.close();
    }

    private static void initPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setBlockWhenExhausted(true);
        pool = new JedisPool(config, "127.0.0.1", 6379, 5000, "liqiyao");
    }
}

二、 redis 方法

public class JedisService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    /**
     * redis set 方法时添加时间 和格式
     * 参数用法 nxxx 对应 NX  XX
     * NX : not exists, 只有key 不存在时才把key value set 到redis
     * XX : is exists ,只有 key 存在是,才把key value set 到redis
     * expx 对应 EX PX  EX是秒,PX是毫秒
    */
    public String set(final String key, final String value, final String nxxx, final String expx,
                      final long time) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key, value,nxxx,expx,time);
        } catch (JedisConnectionException e) {
            jedis.disconnect();
            throw e;
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
    
    /**
    * 获取自增长ID
    * @param redisKey 这个是保存在缓存中的key,所以最好复杂点
    * @return
    */
    public long generateId(String redisKey) {
        RedisAtomicLong counter = new RedisAtomicLong(GENERATE_KEY + redisKey,redisTemplate.getConnectionFactory());
        return counter.incrementAndGet();
    }
    
    
    public void set(String redisKey,int value,long timeout,TimeUnit unit) {
        RedisAtomicLong counter = new RedisAtomicLong(GENERATE_KEY + redisKey, redisTemplate.getConnectionFactory());
        counter.set(value);
        counter.expire(timeout, unit);
    }
    
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key, value);
        } catch (JedisConnectionException e) {
            jedis.disconnect();
            throw e;
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
    
    public Long setnx(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.setnx(key, value);
        } catch (JedisConnectionException e) {
            jedis.disconnect();
            throw e;
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
    
    public String setex(String key, int seconds, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.setex(key, seconds, value);
        } catch (JedisConnectionException e) {
            jedis.disconnect();
            throw e;
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
    /**
    * 成功 1 失败 0
    */
    public Long move(String key, int dbIndex) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.move(key, dbIndex);
        } catch (JedisConnectionException e) {
            jedis.disconnect();
            throw e;
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
}  

三、通过setnx实现分布式锁

https://blog.csdn.net/qq_35406874/article/details/95187102

四、redis 之Redis之Pipeline

https://blog.csdn.net/qq_35406874/article/details/90379102

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值