改进Request类cache方法

This commit is contained in:
thinkphp
2016-09-27 18:33:29 +08:00
parent e8290be253
commit 6278cf13e9
2 changed files with 21 additions and 19 deletions

View File

@@ -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];
}
}

View File

@@ -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']);
}