package cn.reghao.tnb.sb.zk.lock;

import cn.reghao.tnb.sb.zk.redis.ds.RedisCommand;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * 分布式锁
 *
 * @author reghao
 * @date 2019-08-19 10:32:59
 */
public abstract class DistributedLock implements Lock {
    protected RedisCommand commandOps;
    //protected String lockKey;
    protected ThreadLocal<String> lockKey = new ThreadLocal<>();
    protected ThreadLocal<String> lockValue = new ThreadLocal<>();

    public DistributedLock(RedisCommand commandOps, String lockKey) {
        this.commandOps = commandOps;
        //this.lockKey = lockKey;
    }

    protected void setLockValue(String lockValue) {
        this.lockValue.set(lockValue);
    }

    public void lock(String site, String parser) {
        this.lockKey.set("UrlSchedulerKeys.spiderLock(site, parser)");
        lock();
    }

    public void unlock(String site, String parser) {
        this.lockKey.set("UrlSchedulerKeys.spiderLock(site, parser)");
        unlock();
    }

    /**
     * TODO 实现可重入
     *
     * @param
     * @return
     * @date 2019-12-16 下午2:11
     */
    @Override
    public boolean tryLock() {
        return false;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
    }

    @Override
    public Condition newCondition() {
        return null;
    }
}
