From 43b509b0eae6fae9c7796b94b7a642a48ea95362 Mon Sep 17 00:00:00 2001 From: top_nuomi <1130395124@qq.com> Date: Sun, 1 Sep 2019 21:48:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0MySQL=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/Database.php | 10 ++++++++++ framework/library/Model.php | 10 ++++++++++ framework/library/cache/driver/Redis.php | 10 +++++++--- framework/library/database/driver/MySQLi.php | 19 +++++++++++++++++++ framework/middleware/View.php | 4 +--- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/framework/library/Database.php b/framework/library/Database.php index 6b3b8ad..a8136d3 100644 --- a/framework/library/Database.php +++ b/framework/library/Database.php @@ -373,6 +373,16 @@ class Database return $result; } + /** + * MySQL事务 + * @param $action + * @return mixed + */ + public function transaction($action) + { + return self::$driver->transaction($action); + } + /** * 获取最后执行的SQL语句 * diff --git a/framework/library/Model.php b/framework/library/Model.php index d97d736..f7dae76 100644 --- a/framework/library/Model.php +++ b/framework/library/Model.php @@ -303,6 +303,16 @@ class Model return $this->getDb()->query($query); } + /** + * MySQL事务 + * @param $action + * @return mixed + */ + public function transaction($action) + { + return $this->getDb()->transaction($action); + } + /** * 获取最后一次执行的SQL * diff --git a/framework/library/cache/driver/Redis.php b/framework/library/cache/driver/Redis.php index 2990ac4..0534a23 100644 --- a/framework/library/cache/driver/Redis.php +++ b/framework/library/cache/driver/Redis.php @@ -70,10 +70,14 @@ class Redis implements CacheIfs */ public function get($key = null, $callable = null) { - $value = $this->redis->get($key); + $status = $this->exists($key); + $value = $status == 0 ? false : $this->redis->get($key); // 如果获取不到结果但是callable存在 - if ($value === false && is_callable($callable)) { - return $callable($this); + if ($value === false) { + if (is_callable($callable)) { + return $callable($this); + } + return false; } // 判断值是否是json字符串 $jsonDecode = json_decode($value, true); diff --git a/framework/library/database/driver/MySQLi.php b/framework/library/database/driver/MySQLi.php index 12189ca..55c4e0b 100644 --- a/framework/library/database/driver/MySQLi.php +++ b/framework/library/database/driver/MySQLi.php @@ -268,6 +268,25 @@ class MySQLi implements DatabaseIfs return $result; } + /** + * MySQL事务 + * @param $action + * @return bool + * @throws DatabaseException + */ + public function transaction($action) + { + try { + $this->query('start transaction'); + $action(); + $this->query('commit'); + return true; + } catch (DatabaseException $e) { + $this->query('rollback'); + throw new DatabaseException($e->getMessage()); + } + } + /** * 获取执行的最后一条SQL * diff --git a/framework/middleware/View.php b/framework/middleware/View.php index aabf713..5eda809 100644 --- a/framework/middleware/View.php +++ b/framework/middleware/View.php @@ -19,9 +19,7 @@ class View implements MiddlewareIfs $cache = File::instance($config['cacheDir']); if ($cache->exists($ident)) { $content = $cache->get($ident); - return Response::instance()->header([ - 'HTTP/1.1 304 Not Modified' - ])->dispatch($content); + return Response::instance()->dispatch($content); } } return true;