2

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();

有任何想法吗?

4

1 回答 1

3

\Slim\HttpCache\Cache()是用于客户端(浏览器)缓存的 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 响应标头。

它与服务器端缓存无关。

于 2015-12-21T09:39:26.353 回答