RedisLock.php _key = self::PREFIX . $key;$this-_redis = $redis;}/** * 设置锁过期时间 * @param Int $ex */public function setExpires($ex) {$this-_expires = max(1, (int)$ex);return $this;}/** * 开始锁 * @param String $id * @return bo
_key = self::PREFIX . $key; $this->_redis = $redis; } /** * 设置锁过期时间 * @param Int $ex */ public function setExpires($ex) { $this->_expires = max(1, (int)$ex); return $this; } /** * 开始锁 * @param String $id * @return boolean */ public function begin() { $redis = $this->getRedis(); if( $redis->setnx($this->_key, 1) ) { return true; } if( $redis->ttl($this->_key) == -1 ) { $redis->expire($this->_key, $this->_expires); } return false; } /** * 释放 * @param String $id */ public function release() { $this->getRedis()->del($this->_key); } private function getRedis() { return $this->_redis ? $this->_redis : DHTCache::instance()->getNode($this->_key); } } /** $lock = new RedisLock('abc', $redis); $lock->begin(); // ... $lock->release(); */