From 2bd63fec400dee6f1a2ee0f2518c97a33f706141 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 28 Sep 2016 20:57:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=AF=B7=E6=B1=82=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 12 +++++++----- library/think/Response.php | 10 ++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index fac244e8..077e190d 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1495,12 +1495,14 @@ class Request return; } } - if (Cache::has($key)) { + + if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) { // 读取缓存 - $content = Cache::get($key); - $response = Response::create($content) - ->code(304) - ->header('Content-Type', Cache::get($key . '_header')); + $response = Response::create()->code(304); + throw new \think\exception\HttpResponseException($response); + } elseif (Cache::has($key)) { + list($content, $header) = Cache::get($key); + $response = Response::create($content)->header($header); throw new \think\exception\HttpResponseException($response); } else { $this->cache = [$key, $expire]; diff --git a/library/think/Response.php b/library/think/Response.php index d5ab5827..8b62e419 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -111,15 +111,17 @@ class Response header($name . ':' . $val); } } - 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']); + header('Cache-Control: max-age=' . $cache[1] . ',must-revalidate'); + header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT'); + header('Expires:' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT'); + $header['Content-Type'] = $this->header['Content-Type']; + Cache::set($cache[0], [$data, $header], $cache[1]); } } + echo $data; if (function_exists('fastcgi_finish_request')) { // 提高页面响应