mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进Request类cache方法
This commit is contained in:
@@ -1476,26 +1476,28 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public function cache($key, $expire = null)
|
public function cache($key, $expire = null)
|
||||||
{
|
{
|
||||||
if (false !== strpos($key, ':')) {
|
if ($this->isGet()) {
|
||||||
$param = $this->param();
|
if (false !== strpos($key, ':')) {
|
||||||
foreach ($param as $item => $val) {
|
$param = $this->param();
|
||||||
if (is_string($val) && false !== strpos($key, ':' . $item)) {
|
foreach ($param as $item => $val) {
|
||||||
$key = str_replace(':' . $item, $val, $key);
|
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];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class Response
|
|||||||
|
|
||||||
if (200 == $this->code) {
|
if (200 == $this->code) {
|
||||||
$cache = Request::instance()->getCache();
|
$cache = Request::instance()->getCache();
|
||||||
if ($cache && Request::instance()->isGet()) {
|
if ($cache) {
|
||||||
Cache::set($cache[0], $data, $cache[1]);
|
Cache::set($cache[0], $data, $cache[1]);
|
||||||
Cache::set($cache[0] . '_header', $this->header['Content-Type']);
|
Cache::set($cache[0] . '_header', $this->header['Content-Type']);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user