From a6363c2cd3a0684ea622d534b4b5772c3e880ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Sep 2020 15:19:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Cache/Redis.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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