Пример #1
0
 /**
  * 释放资源
  *
  * @param key
  */
 public void releaseLock(final String key) {
   try {
     if (redisCacheDao.existsKey(key)) ;
     redisCacheDao.delKey(key);
   } catch (Exception e) {
     log.error("", e);
   }
 }
Пример #2
0
  /**
   * 锁资源
   *
   * @param key
   * @return true获取锁 反之没有获取锁
   * @throws InterruptedException
   */
  public boolean getLock(final String key) throws InterruptedException {
    String keySeed = "lock:" + key;
    String value = System.currentTimeMillis() + "|" + timeout;
    return redisCacheDao.setNx(keySeed, value);

    /*try {
    while (true) {
        setnxResult = redisCacheDao.setNx(keySeed,value);
        if (setnxResult) {
            redisCacheDao.expire(keySeed,1);
            return true;
        } else  {
            return false;
            */
    /*returnValue = redisCacheDao.get(keySeed);
    long currentTime = System.currentTimeMillis();
    while (true) {
        splitString = returnValue.split("[|]");
        if (currentTime < Long.parseLong(splitString[0])
                + Long.parseLong(splitString[1])) {
            if (++tRetry > retry)
                return false;
            Thread.sleep(sleepTime);
            returnValue = redisCacheDao.get(keySeed);
            currentTime = System.currentTimeMillis();
            continue;
        } else {
            value = System.currentTimeMillis() + "|" + timeout;
            String oldValue = redisCacheDao.getSet(keySeed,value);
            if (returnValue.equals(oldValue)) {
                return true;
            } else {
                break;
            }
        }
    }*/
    /*
            }
        }
    } catch (Exception e) {
        log.error("[LockCacheImpl]->[lock] error:[keySeed]=" + keySeed, e);
        return false;
    }*/
  }