mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
增加请求缓存功能 路由支持设置请求缓存
This commit is contained in:
@@ -119,6 +119,8 @@ class Request
|
||||
protected $bind = [];
|
||||
// php://input
|
||||
protected $input;
|
||||
// 请求缓存
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
@@ -1453,6 +1455,30 @@ class Request
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $key 缓存标识
|
||||
* @param mixed $expire 静态有效期
|
||||
* @return mixed
|
||||
*/
|
||||
public function cache($key, $expire = null)
|
||||
{
|
||||
if (Cache::has($key)) {
|
||||
// 读取缓存
|
||||
$content = Cache::get($key);
|
||||
$response = Response::create($content)->header('Content-Type', Cache::get($key . '_header'));
|
||||
throw new \think\exception\HttpResponseException($response);
|
||||
} else {
|
||||
$this->cache = [$key, $expire];
|
||||
}
|
||||
}
|
||||
|
||||
public function getCache()
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前请求绑定的对象实例
|
||||
* @access public
|
||||
|
||||
@@ -11,9 +11,11 @@
|
||||
|
||||
namespace think;
|
||||
|
||||
use think\Cache;
|
||||
use think\Config;
|
||||
use think\Debug;
|
||||
use think\Env;
|
||||
use think\Request;
|
||||
use think\response\Json as JsonResponse;
|
||||
use think\response\Jsonp as JsonpResponse;
|
||||
use think\response\Redirect as RedirectResponse;
|
||||
@@ -111,6 +113,14 @@ class Response
|
||||
}
|
||||
echo $data;
|
||||
|
||||
if (200 == $this->code) {
|
||||
$cache = Request::instance()->getCache();
|
||||
if ($cache) {
|
||||
Cache::set($cache[0], $data, $cache[1]);
|
||||
Cache::set($cache[0] . '_header', $this->header['Content-Type']);
|
||||
}
|
||||
}
|
||||
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
// 提高页面响应
|
||||
fastcgi_finish_request();
|
||||
|
||||
@@ -1292,6 +1292,7 @@ class Route
|
||||
*/
|
||||
private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = [], $merge = false)
|
||||
{
|
||||
$request = Request::instance();
|
||||
// 解析路由规则
|
||||
if ($rule) {
|
||||
$rule = explode('/', $rule);
|
||||
@@ -1364,13 +1365,13 @@ class Route
|
||||
$bind[$key] = $result;
|
||||
}
|
||||
}
|
||||
Request::instance()->bind($bind);
|
||||
$request->bind($bind);
|
||||
}
|
||||
|
||||
// 解析额外参数
|
||||
self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
|
||||
// 记录匹配的路由信息
|
||||
Request::instance()->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]);
|
||||
$request->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]);
|
||||
|
||||
// 检测路由after行为
|
||||
if (!empty($option['after_behavior'])) {
|
||||
@@ -1410,6 +1411,10 @@ class Route
|
||||
// 路由到模块/控制器/操作
|
||||
$result = self::parseModule($route);
|
||||
}
|
||||
// 开启请求缓存
|
||||
if ($request->isGet() && !empty($option['cache'])) {
|
||||
$request->cache($pathinfo, $option['cache']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user