for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// | Author: liu21st <[email protected]>
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\Cache;
use think\Request;
use think\Response;
class CheckRequestCache
{
protected $cache;
public function __construct(Cache $cache)
$this->cache = $cache;
}
/**
$next
* 设置当前地址的请求缓存
* @access public
* @param Request $request
* @param $next
* @return Response
*/
public function handle($request, Closure $next)
$cache = $request->cache();
if ($cache) {
list($key, $expire, $tag) = $cache;
if (strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $request->server('REQUEST_TIME')) {
// 读取缓存
return Response::create()->code(304);
} elseif ($this->cache->has($key)) {
list($content, $header) = $this->cache->get($key);
return Response::create($content)->header($header);
return $next($request);