HTTP 缓存有什么用?如何在 Slim 3 中使用它?
但我不太确定如何在 Slim 3 中做到这一点:
use Slim\Http\Request;
use Slim\Http\Response;
require_once __DIR__ . '/../vendor/autoload.php';
// Register service provider with the container
$container = new \Slim\Container;
$container['cache'] = function () {
return new \Slim\HttpCache\CacheProvider();
};
$app = new \Slim\App($container);
// Add middleware to the application.
$app->add(new \Slim\HttpCache\Cache('cache', 86400));
// Routes:
$app->get('/', function (Request $request, Response $response, array $args) {
$response->getBody()->write('Hello, World!');
return $response->withHeader('Content-type', 'application/json');
});
$app->get('/foo', function ($req, $res, $args) {
$resWithEtag = $this->cache
->withEtag($res, 'abc')
// ->withExpires($res, time() + 60)
;
return $resWithEtag;
});
$app->run();
有任何想法吗?