Slim 3:什么是 HTTP 缓存?
Slim 3: what is HTTP Cache for?
HTTP 缓存有什么用?如何在 Slim 3 中使用它?
但我不太清楚 this is done in 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();
有什么想法吗?
\Slim\HttpCache\Cache()
是 client-side(浏览器)缓存的 HTTP 缓存
最多需要 3 个参数:
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param bool $mustRevalidate must-revalidate
并生成对应的HTTP-responseheaders.
It has nothing with server-side caching to do.
HTTP 缓存有什么用?如何在 Slim 3 中使用它?
但我不太清楚 this is done in 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();
有什么想法吗?
\Slim\HttpCache\Cache()
是 client-side(浏览器)缓存的 HTTP 缓存
最多需要 3 个参数:
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param bool $mustRevalidate must-revalidate
并生成对应的HTTP-responseheaders.
It has nothing with server-side caching to do.