【工作笔记】redis异步锁

遇到了资源竞争的问题,刚好项目有用redis,就做一个吧。项目用了spring-data-redis,锁名的结构是lock:xxid:xxid

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String acquireLock(int timeout, int repertoryId, int productId) {
        String identifier = UUID.randomUUID().toString();
        String lockName = String.format(Constants.STORK_LOCK, repertoryId, productId);
        long endTime = System.currentTimeMillis() + (timeout * 1000);
        ValueOperations svo = stringRedisTemplate.opsForValue();

        while (System.currentTimeMillis() < endTime) {
            if (svo.setIfAbsent(lockName, identifier).booleanValue()){
                stringRedisTemplate.expire(lockName, timeout, TimeUnit.SECONDS);
                return identifier;
            } else if (stringRedisTemplate.getExpire(lockName) != -2) {
                //当锁已经被占用,但是超过超时时间时,为其重置超时时间
                stringRedisTemplate.expire(lockName, timeout, TimeUnit.SECONDS);
            } else {
                try {
                    //等待100毫秒
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        return "";
    }

    @Override
    public boolean releaseLock(String id, int repertoryId, int productId) {
        String lockName = String.format(Constants.STORK_LOCK, repertoryId, productId);
        ValueOperations svo = stringRedisTemplate.opsForValue();
        while (true) {
            try {
                stringRedisTemplate.watch(lockName);
                if (svo.get(lockName).equals(id)) {
                    stringRedisTemplate.delete(lockName);
                    return true;
                } else {
                    stringRedisTemplate.unwatch();
                    break;
                }
            } catch (Exception e) {
                logger.error("其他客户端修改了锁,正在重试.", e);
            }
        }
        return false;
    }

你可能感兴趣的:(bug猿的自我修养)