2
什麼是HTTP緩存?我如何在Slim 3中使用它?Slim 3:什麼是HTTP Cache?
但我不太清楚如何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();
任何想法?
是HTTP緩存不同於我正在尋找? – laukok
我認爲這是非常不同的。瀏覽器緩存可在您的瀏覽器中運行。要緩存某些內容,您只需訪問一次網頁,然後,您是否再次訪問此頁面,如果緩存仍然有效,瀏覽器將不會發送新的請求。它最適合靜態文件,如CSS,JavaScript和圖像。服務器端緩存完全不同。您可以緩存以節省昂貴的數據庫請求或計算時間。對於第一個訪問者來說,它會很慢,對於其他人來說可能會更快......您還應該瞭解何時緩存或分散緩存...... –