【问题标题】:Slim 3: what is HTTP Cache for?Slim 3:什么是 HTTP 缓存?
【发布时间】:2015-12-23 12:59:51
【问题描述】:

什么是 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();

有什么想法吗?

【问题讨论】:

    标签: php caching slim slim-3


    【解决方案1】:

    \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 响应标头。

    它与服务器端缓存无关。

    【讨论】:

    • HTTP 缓存与我要查找的不同吗?
    • 我认为这是非常不同的。浏览器缓存在您的浏览器中工作。要缓存某些内容,您已经访问过一次网页,那么您是否再次访问该页面,如果缓存仍然有效,浏览器将不会发送新请求。它最适用于 css、javascript 和图像等静态文件。服务器端缓存完全不同。您缓存以节省昂贵的数据库请求或计算的时间。对于第一个访问者,它会很慢,对于其他访问者 - 可能会更快...您还应该了解何时缓存或区分缓存...
    猜你喜欢
    • 2015-06-23
    • 2010-10-07
    • 2014-03-29
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多