From 450d7ab313da3ab50f4e0143ab28cabf0f7f825f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 24 Sep 2016 18:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=B7=E6=B1=82=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=8A=9F=E8=83=BD=20=E8=B7=AF=E7=94=B1=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AE=BE=E7=BD=AE=E8=AF=B7=E6=B1=82=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 26 ++++++++++++++++++++++++++ library/think/Response.php | 10 ++++++++++ library/think/Route.php | 9 +++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index fb39356f..825d2a0f 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -119,6 +119,8 @@ class Request protected $bind = []; // php://input protected $input; + // 请求缓存 + protected $cache; /** * 架构函数 @@ -1453,6 +1455,30 @@ class Request return $token; } + /** + * 读取缓存 + * @access public + * @param string $key 缓存标识 + * @param mixed $expire 静态有效期 + * @return mixed + */ + public function cache($key, $expire = null) + { + if (Cache::has($key)) { + // 读取缓存 + $content = Cache::get($key); + $response = Response::create($content)->header('Content-Type', Cache::get($key . '_header')); + throw new \think\exception\HttpResponseException($response); + } else { + $this->cache = [$key, $expire]; + } + } + + public function getCache() + { + return $this->cache; + } + /** * 设置当前请求绑定的对象实例 * @access public diff --git a/library/think/Response.php b/library/think/Response.php index 35cafe90..7a58ca65 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -11,9 +11,11 @@ namespace think; +use think\Cache; use think\Config; use think\Debug; use think\Env; +use think\Request; use think\response\Json as JsonResponse; use think\response\Jsonp as JsonpResponse; use think\response\Redirect as RedirectResponse; @@ -111,6 +113,14 @@ class Response } echo $data; + if (200 == $this->code) { + $cache = Request::instance()->getCache(); + if ($cache) { + Cache::set($cache[0], $data, $cache[1]); + Cache::set($cache[0] . '_header', $this->header['Content-Type']); + } + } + if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); diff --git a/library/think/Route.php b/library/think/Route.php index 1963d9e2..12368037 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -1292,6 +1292,7 @@ class Route */ private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = [], $merge = false) { + $request = Request::instance(); // 解析路由规则 if ($rule) { $rule = explode('/', $rule); @@ -1364,13 +1365,13 @@ class Route $bind[$key] = $result; } } - Request::instance()->bind($bind); + $request->bind($bind); } // 解析额外参数 self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches); // 记录匹配的路由信息 - Request::instance()->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]); + $request->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]); // 检测路由after行为 if (!empty($option['after_behavior'])) { @@ -1410,6 +1411,10 @@ class Route // 路由到模块/控制器/操作 $result = self::parseModule($route); } + // 开启请求缓存 + if ($request->isGet() && !empty($option['cache'])) { + $request->cache($pathinfo, $option['cache']); + } return $result; }