From 57f206b75744464778dcc76de00756591b6dc3fb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 1 Nov 2016 17:51:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E8=AF=B7=E6=B1=82=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=8A=9F=E8=83=BD=20=E6=94=AF=E6=8C=81=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=85=8D=E7=BD=AE=20=E5=A2=9E=E5=8A=A0request=5Fcache?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E7=BC=93=E5=AD=98=E8=A7=84=E5=88=99=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20=E5=A6=82=E6=9E=9C=E5=90=8C=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=AD=98=E5=9C=A8=E5=A4=9A=E6=AC=A1=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=BC=93=E5=AD=98=E8=B0=83=E7=94=A8=20=E5=88=99?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=9C=89=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 2 ++ library/think/App.php | 2 ++ library/think/Request.php | 26 ++++++++++++++++++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/convention.php b/convention.php index 2ef29dcd..23408722 100644 --- a/convention.php +++ b/convention.php @@ -103,6 +103,8 @@ return [ 'var_ajax' => '_ajax', // 表单pjax伪装变量 'var_pjax' => '_pjax', + // 是否开启请求缓存 true自动缓存 支持设置请求缓存规则 + 'request_cache' => false, // +---------------------------------------------------------------------- // | 模板设置 diff --git a/library/think/App.php b/library/think/App.php index 7feff2fe..aed3327e 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -115,6 +115,8 @@ class App // 进行URL路由检测 $dispatch = self::routeCheck($request, $config); } + // 请求缓存检查 + $request->cache($config['request_cache']); // 记录当前调度信息 $request->dispatch($dispatch); diff --git a/library/think/Request.php b/library/think/Request.php index 634746c9..b55db39c 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -121,6 +121,8 @@ class Request protected $input; // 请求缓存 protected $cache; + // 缓存是否检查 + protected $isCheckCache; /** * 架构函数 @@ -1479,7 +1481,20 @@ class Request */ public function cache($key, $expire = null) { - if ($this->isGet()) { + if (false !== $key && $this->isGet() && !$this->isCheckCache) { + if ($key instanceof \Closure) { + $key = call_user_func_array($key, [$this]); + } elseif (true === $key) { + // 自动缓存功能 + $key = '__URL__'; + } elseif (strpos($key, '|')) { + list($key, $fun) = explode('|', $key); + } + // 特殊规则替换 + if (false !== strpos($key, '__')) { + $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url())], $key); + } + if (false !== strpos($key, ':')) { $param = $this->param(); foreach ($param as $item => $val) { @@ -1487,9 +1502,6 @@ class Request $key = str_replace(':' . $item, $val, $key); } } - } elseif ('__URL__' == $key) { - // 当前URL地址作为缓存标识 - $key = md5($this->url()); } elseif (strpos($key, ']')) { if ('[' . $this->ext() . ']' == $key) { // 缓存某个后缀的请求 @@ -1498,6 +1510,12 @@ class Request return; } } + if (isset($fun)) { + $key = $fun($key); + } + + // 标记请求缓存检查 + $this->isCheckCache = true; if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) { // 读取缓存