69.安心技术梳理 - Redis实现【分布式锁】工具,含超时时间/等待时间干货

1.创建引入一个Redis工具方法类

2.实现工具 - redisClient为工具

String lockKey = "key"; 1000L后获取锁,20000L维持锁存在总时长
DistributedLocker.instance(redisClient)
        .lock(lockKey, 1000L, 20000L,
                () -> this.method(提交方法));

实现工具类:

import com.RedisClient;
import com.utils.RetryUtils;

import java.util.concurrent.TimeUnit;

//分布式锁
public class DistributedLocker {

    private static final Long DEFAULT_WAIT_TIME = 1000L;
    private static final Long DEFAULT_EXPIRE_TIME = 10000L;

    private RedisClient redisClient;

    private DistributedLocker(RedisClient redisClient) throws DistributedLockException {
        if (redisClient == null) {
            throw new DistributedLockException("Distributed lock, redisClient must not null");
        }
        this.redisClient = redisClient;
    }

    public static DistributedLocker instance(RedisClient redisClient) {
        return new DistributedLocker(redisClient);
    }

    public <T> T lock(String key, Long waitTimeMilliseconds, Long expireTimeMilliseconds, LockCallback<T> callback) {
        if (waitTimeMilliseconds == null) {
            waitTimeMilliseconds = DEFAULT_WAIT_TIME;
        }
        if (expireTimeMilliseconds == null) {
            expireTimeMilliseconds = DEFAULT_EXPIRE_TIME;
        }
        paramCheck(key, waitTimeMilliseconds, expireTimeMilliseconds, callback);
        boolean success = false;
        try {
            long interval = 10L;
            long tryTimes = waitTimeMilliseconds / interval;
            do {
                success = redisClient.set(key, "1", expireTimeMilliseconds, TimeUnit.MILLISECONDS, false);
                if (success) {
                    return callback.call();
                } else {
                    try {
                        Thread.sleep(interval);
                    } catch (InterruptedException e) {
                        //do nothing
                    }
                }
            } while (--tryTimes > 0);
        } finally {
            if (success) {
                RetryUtils.retryQuietly(3, () -> redisClient.del(key));
            }
        }
        return null;
    }

    private <T> void paramCheck(String key, Long waitTimeMilliseconds, Long expireTimeMilliseconds,
                                LockCallback<T> callback) {
        if (key == null || key.trim().length() == 0) {
            throw new DistributedLockException("Distributed lock, key can not null");
        }
        if (waitTimeMilliseconds <= 0) {
            throw new DistributedLockException("Distributed lock, waitTimeMilliseconds must larger than 0");
        }
        if (expireTimeMilliseconds <= 0) {
            throw new DistributedLockException("Distributed lock, expireTimeMilliseconds must larger than 0");
        }
        if (callback == null) {
            throw new DistributedLockException("Distributed lock, callback must not null");
        }
    }

}

辅助工具1

public class DistributedLockException extends RuntimeException {

    public DistributedLockException(String msg) {
        super(msg);
    }
}

辅助工具2

//回调函数

public interface LockCallback<T> {

    T call();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值