diff --git a/library/think/Request.php b/library/think/Request.php index 92a82cbf..924f2e8f 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1476,26 +1476,28 @@ class Request */ public function cache($key, $expire = null) { - if (false !== strpos($key, ':')) { - $param = $this->param(); - foreach ($param as $item => $val) { - if (is_string($val) && false !== strpos($key, ':' . $item)) { - $key = str_replace(':' . $item, $val, $key); + if ($this->isGet()) { + if (false !== strpos($key, ':')) { + $param = $this->param(); + foreach ($param as $item => $val) { + if (is_string($val) && false !== strpos($key, ':' . $item)) { + $key = str_replace(':' . $item, $val, $key); + } } + } elseif ('__URL__' == $key) { + // 当前URL地址作为缓存标识 + $key = $this->url(); + } + if (Cache::has($key)) { + // 读取缓存 + $content = Cache::get($key); + $response = Response::create($content) + ->code(304) + ->header('Content-Type', Cache::get($key . '_header')); + throw new \think\exception\HttpResponseException($response); + } else { + $this->cache = [$key, $expire]; } - } elseif ('__URL__' == $key) { - // 当前URL地址作为缓存标识 - $key = $this->url(); - } - if (Cache::has($key)) { - // 读取缓存 - $content = Cache::get($key); - $response = Response::create($content) - ->code(304) - ->header('Content-Type', Cache::get($key . '_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 4b96b32d..d5ab5827 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -115,7 +115,7 @@ class Response if (200 == $this->code) { $cache = Request::instance()->getCache(); - if ($cache && Request::instance()->isGet()) { + if ($cache) { Cache::set($cache[0], $data, $cache[1]); Cache::set($cache[0] . '_header', $this->header['Content-Type']); }