private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 * 1000L;// 1hour /** * 获取redis分布式锁 * * @author Administrator * @version 1.0 * @return true:获得锁权限 false:未获得锁权限 */ private synchronized boolean getRedLock() { Long flag = jedis.setnx(RedisLockKey, String.valueOf(System.currentTimeMillis() + altTimeout + 1L)); if (flag == 0L) { String value = jedis.get(RedisLockKey); long longVal = Long.parseLong(value); if (longVal > System.currentTimeMillis()) {// 未过期 return Boolean.FALSE; } else {// 过期 String _value = jedis.getSet(RedisLockKey, String.valueOf(System.currentTimeMillis() + altTimeout + 1L)); long _longVal = Long.parseLong(_value); if (_longVal > System.currentTimeMillis()) { return Boolean.FALSE; } else { return Boolean.TRUE; } } } else { return Boolean.TRUE; } }
时间: 2024-11-08 21:39:59