mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
增强请求缓存功能 支持全局配置 增加request_cache配置参数 增加特殊缓存规则定义 如果同一次请求存在多次请求缓存调用 则第一次有效
This commit is contained in:
@@ -103,6 +103,8 @@ return [
|
|||||||
'var_ajax' => '_ajax',
|
'var_ajax' => '_ajax',
|
||||||
// 表单pjax伪装变量
|
// 表单pjax伪装变量
|
||||||
'var_pjax' => '_pjax',
|
'var_pjax' => '_pjax',
|
||||||
|
// 是否开启请求缓存 true自动缓存 支持设置请求缓存规则
|
||||||
|
'request_cache' => false,
|
||||||
|
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | 模板设置
|
// | 模板设置
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ class App
|
|||||||
// 进行URL路由检测
|
// 进行URL路由检测
|
||||||
$dispatch = self::routeCheck($request, $config);
|
$dispatch = self::routeCheck($request, $config);
|
||||||
}
|
}
|
||||||
|
// 请求缓存检查
|
||||||
|
$request->cache($config['request_cache']);
|
||||||
// 记录当前调度信息
|
// 记录当前调度信息
|
||||||
$request->dispatch($dispatch);
|
$request->dispatch($dispatch);
|
||||||
|
|
||||||
|
|||||||
@@ -121,6 +121,8 @@ class Request
|
|||||||
protected $input;
|
protected $input;
|
||||||
// 请求缓存
|
// 请求缓存
|
||||||
protected $cache;
|
protected $cache;
|
||||||
|
// 缓存是否检查
|
||||||
|
protected $isCheckCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 架构函数
|
* 架构函数
|
||||||
@@ -1479,7 +1481,20 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public function cache($key, $expire = null)
|
public function cache($key, $expire = null)
|
||||||
{
|
{
|
||||||
if ($this->isGet()) {
|
if (false !== $key && $this->isGet() && !$this->isCheckCache) {
|
||||||
|
if ($key instanceof \Closure) {
|
||||||
|
$key = call_user_func_array($key, [$this]);
|
||||||
|
} elseif (true === $key) {
|
||||||
|
// 自动缓存功能
|
||||||
|
$key = '__URL__';
|
||||||
|
} elseif (strpos($key, '|')) {
|
||||||
|
list($key, $fun) = explode('|', $key);
|
||||||
|
}
|
||||||
|
// 特殊规则替换
|
||||||
|
if (false !== strpos($key, '__')) {
|
||||||
|
$key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url())], $key);
|
||||||
|
}
|
||||||
|
|
||||||
if (false !== strpos($key, ':')) {
|
if (false !== strpos($key, ':')) {
|
||||||
$param = $this->param();
|
$param = $this->param();
|
||||||
foreach ($param as $item => $val) {
|
foreach ($param as $item => $val) {
|
||||||
@@ -1487,9 +1502,6 @@ class Request
|
|||||||
$key = str_replace(':' . $item, $val, $key);
|
$key = str_replace(':' . $item, $val, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ('__URL__' == $key) {
|
|
||||||
// 当前URL地址作为缓存标识
|
|
||||||
$key = md5($this->url());
|
|
||||||
} elseif (strpos($key, ']')) {
|
} elseif (strpos($key, ']')) {
|
||||||
if ('[' . $this->ext() . ']' == $key) {
|
if ('[' . $this->ext() . ']' == $key) {
|
||||||
// 缓存某个后缀的请求
|
// 缓存某个后缀的请求
|
||||||
@@ -1498,6 +1510,12 @@ class Request
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (isset($fun)) {
|
||||||
|
$key = $fun($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 标记请求缓存检查
|
||||||
|
$this->isCheckCache = true;
|
||||||
|
|
||||||
if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
|
if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
|
||||||
// 读取缓存
|
// 读取缓存
|
||||||
|
|||||||
Reference in New Issue
Block a user