What is HTTP Cache for? How do I use it in Slim 3?
But I am not quite sure how 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();
Any ideas?
\Slim\HttpCache\Cache()
is HTTP Cache for client-side (browser) caching
It expects up to 3 Parameters:
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param bool $mustRevalidate must-revalidate
and generates corresponding HTTP-response headers.
It has nothing with server-side caching to do.