Redis 工具类 +锁

Redis - String

Redis - List

//list删除指定元素
public final Long lrem(String key, long count, String value) {
    Jedis redis = this.getRedis();
    Long var5;
    try {
        var5 = redis.lrem(key, count, value);
    } finally {
        this.release(redis);
    }
    return var5;
}
    
/**
 * 从列表中从头部开始移除count个匹配的值。如果count为零,所有匹配的元素都被删除。如果count是负数,内容从尾部开始删除。
 */
public final Long lrem(String key, Long count, String value) {
    Jedis redis = getRedis();
    Long result = redis.lrem(key, count, value);
    redis.close();
    return result;
}

Redis - Hash

Redis - Set

Redis - ZSet

//zset 查询指定key的总条数
public final Long zcard(String key) {
    Jedis redis = this.getRedis();
    Long zcard = redis.zcard(key);
    redis.close();
    return zcard;
}

//zset  获取指定key的所有数据
public Set<Tuple> zrangeWithScoresAll(String key) {
    return this.zrangeWithScores(key, 0L, -1L);
}

//zset 获取指定key的分数区间的值
public final Set<Tuple> zrangeWithScores(String key, Long min, Long max) {
    Jedis redis = this.getRedis();
    Set<Tuple> var5;
    try {
        var5 = redis.zrangeWithScores(key, min, max);
    } finally {
        this.release(redis);
    }
    return var5;
}

//zset 获取指定分数的值
public final Set<String> zrangebyscorezd(String key, long score) {
    return zrangeByScore(key, score, score);
}

//zset 获取分数区间的值
public final Set<String> zrangeByScore(String key, long min, long max) {
    Jedis redis = this.getRedis();
    Set<String> var5;
    try {
        var5 = redis.zrangeByScore(key, min, max);
    } finally {
        this.release(redis);
    }

    return var5;
}


//zset 删除指定分数的值
public final long zremrangeByScore(String key, String score) {
    return zremrangeByScore(key, score, score);
}

//zset 删除分数区间的值
public final long zremrangeByScore(String key, String min, String max) {
    Jedis redis = this.getRedis();
    long var5;
    try {
        var5 = redis.zremrangeByScore(key, min, max);
    } finally {
        this.release(redis);
    }

    return var5;zrange mZySet 0 -1 withscores
}

/**
 * Redis Zrevrangebyscore 返回有序集中指定分数区间内的所有的成员。有序集成员按分数值递减(从大到小)的次序排列。
 * 具有相同分数值的成员按字典序的逆序(reverse lexicographical order )排列。
 * 除了成员按分数值递减的次序排列这一点外, ZREVRANGEBYSCORE 命令的其他方面和 ZRANGEBYSCORE 命令一样。
 *
 * @param key
 * @param max
 * @param min
 * @param offset
 * @param count
 * @return 指定区间内,带有分数值(可选)的有序集成员的列表。
 */
public final Set<String> zrevrangeByScore(String key, String max, String min, int offset, int count) {
    Jedis redis = this.getRedis();
    Set<String> strings = redis.zrevrangeByScore(key, max, min, offset, count);
    redis.close();
    return strings;
}

Redis - 锁

/**
 * 加锁 测试
 */
public void ceshi() {
    String token = null;
    String suoiD = "";
    try {
        token = lock(suoiD, 10000, 10000 + 100);
        if (token != null) {
            System.out.println("拿到了业务锁哦!");

        } else {
            System.out.println("没有拿到锁!");
        }
    } catch (Exception e) {

    } finally {
        System.out.println("开始解锁!");
        if (token != null) {
            unlock(suoiD, token);
        }
    }
}


/**
 * 加锁,有阻塞
 *
 * @param name    加锁参数
 * @param expire  超时时间
 * @param timeout 阻塞时间
 * @return
 */
public String lock(String name, long expire, long timeout) {
    //限制阻塞时间,根据自己的业务系统设置。如果尝试加锁的线程多的话最好不要设置的太大,要不然会有太多的线程在自旋,耗费CPU
    Assert.isTrue(timeout > 0 && timeout < 60000, "timeout must greater than 0 and less than 1 min");
    long startTime = System.currentTimeMillis();
    String token;
    do {
        token = tryLock(name, expire);
        //加锁失败
        if (token == null) {
            //超过阻塞时间则跳出
            if ((System.currentTimeMillis() - startTime) > (timeout - 50))
                break;
            try {
                //等待50ms再试,线程多的话这个值不建议设置的太小
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }
    } while (token == null);

    return token;
}

/**
 * 加锁,无阻塞
 *
 * @param name
 * @param expire
 * @return
 */
public String tryLock(String name, long expire) {
    Assert.notNull(name, "Lock name must not be null");
    Assert.isTrue(expire > 0, "expire must greater than 0");
    String token = IdService.newUUID();
    //获取redis连接
    Jedis redis = this.getRedis();
    try {
        //原子操作,如果key不存在则set,并设置超时时间
        SetParams setParams = new SetParams();
        setParams.px(expire);
        setParams.nx();
        String result = redis.set(name.getBytes(Charset.forName("UTF-8")), token.getBytes(Charset.forName("UTF-8")), setParams);
        if (result != null)
            return token;
    } finally {
        //释放连接
        redis.close();
    }
    return null;
}

/**
 * 解锁
 *
 * @param name
 * @param token
 * @return
 */
public boolean unlock(String name, String token) {
    //解锁脚本,原子操作
    Jedis redis = this.getRedis();
    try {
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        Object result = redis.eval(script, Collections.singletonList(name), Collections.singletonList(token));
        if (RELEASE_SUCCESS.equals(result)) {
            return true;
        }
    } finally {
        redis.close();
    }
    return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值