From 04ee0166430dad3fcc28eb14059d44afa989b291 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 17 Aug 2017 16:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=B1=BB=E7=9A=84remember?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=A2=9E=E5=8A=A0=E9=94=81=E5=AE=9A=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/cache/Driver.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/library/think/cache/Driver.php b/library/think/cache/Driver.php index c87d8034..731a6933 100644 --- a/library/think/cache/Driver.php +++ b/library/think/cache/Driver.php @@ -120,10 +120,23 @@ abstract class Driver public function remember($name, $value, $expire = null) { if (!$this->has($name)) { - if ($value instanceof \Closure) { - $value = call_user_func($value); + while ($this->has($name . '.lock')) { + // 存在锁定则等待 + } + + try { + // 锁定 + $this->set($name . '.lock', true); + if ($value instanceof \Closure) { + $value = call_user_func($value); + } + $this->set($name, $value, $expire); + // 解锁 + $this->rm($name . '.lock'); + } catch (\Exception $e) { + // 解锁 + $this->rm($name . '.lock'); } - $this->set($name, $value, $expire); } else { $value = $this->get($name); }