全局请求缓存添加排除规则 添加request_cache_except配置参数

This commit is contained in:
thinkphp
2017-01-26 20:38:22 +08:00
parent 13c7eb8452
commit 79fb229181
3 changed files with 11 additions and 3 deletions

View File

@@ -107,6 +107,8 @@ return [
'request_cache' => false, 'request_cache' => false,
// 请求缓存有效期 // 请求缓存有效期
'request_cache_expire' => null, 'request_cache_expire' => null,
// 全局请求缓存排除规则
'request_cache_except' => [],
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | 模板设置 // | 模板设置

View File

@@ -118,7 +118,7 @@ class App
// 监听app_begin // 监听app_begin
Hook::listen('app_begin', $dispatch); Hook::listen('app_begin', $dispatch);
// 请求缓存检查 // 请求缓存检查
$request->cache($config['request_cache'], $config['request_cache_expire']); $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
switch ($dispatch['type']) { switch ($dispatch['type']) {
case 'redirect': case 'redirect':
@@ -336,7 +336,7 @@ class App
$request->module($module); $request->module($module);
$config = self::init($module); $config = self::init($module);
// 模块请求缓存检查 // 模块请求缓存检查
$request->cache($config['request_cache'], $config['request_cache_expire']); $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
} else { } else {
throw new HttpException(404, 'module not exists:' . $module); throw new HttpException(404, 'module not exists:' . $module);
} }

View File

@@ -1502,9 +1502,10 @@ class Request
* @access public * @access public
* @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
* @param mixed $expire 缓存有效期 * @param mixed $expire 缓存有效期
* @param array $except 缓存排除
* @return void * @return void
*/ */
public function cache($key, $expire = null) public function cache($key, $expire = null, $except = [])
{ {
if (false !== $key && $this->isGet() && !$this->isCheckCache) { if (false !== $key && $this->isGet() && !$this->isCheckCache) {
// 标记请求缓存检查 // 标记请求缓存检查
@@ -1516,6 +1517,11 @@ class Request
if ($key instanceof \Closure) { if ($key instanceof \Closure) {
$key = call_user_func_array($key, [$this]); $key = call_user_func_array($key, [$this]);
} elseif (true === $key) { } elseif (true === $key) {
foreach ($except as $rule) {
if (0 === strpos($this->url(), $rule)) {
return;
}
}
// 自动缓存功能 // 自动缓存功能
$key = '__URL__'; $key = '__URL__';
} elseif (strpos($key, '|')) { } elseif (strpos($key, '|')) {