diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index 6946a9ca..df6ffa02 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -286,6 +286,36 @@ class Redis extends Component return $data; } + + /** + * @param $key + * @param int $timeout + * @return bool + * @throws Exception + */ + public function lock($key, $timeout = 5) + { + $redis = $this->proxy(); + if (!$redis->setnx($key, 1)) { + return false; + } + $redis->expire($key, $timeout); + return true; + } + + + /** + * @param $key + * @return int + * @throws Exception + */ + public function unlock($key) + { + $redis = $this->proxy(); + return $redis->del($key); + } + + /** * 释放连接池 * @throws ConfigException