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']) { // 读取缓存