shawnley/laravel-httpcache

Laravel 5 的 HttpCache

0.2.5 2015-06-29 14:09 UTC

This package is not auto-updated.

Last update: 2024-09-18 08:22:09 UTC


README

对于 Laravel 4.1+,需要 v0.1.x

Laravel 5 可以使用 HttpKernelInterface 中间件,因此也可以使用 HttpCache。此包提供了一个简单的 ServiceProvider,帮助您开始使用 HttpCache。

首先,在 composer.json 中要求此包,并运行 composer update

"barryvdh/laravel-httpcache": "0.2.x@dev"

更新后,将 ServiceProvider 添加到 app/config/app.php 中的提供者数组中

'Barryvdh\HttpCache\ServiceProvider',

现在您可以将 Middleware 添加到您的 Kernel 中

'Barryvdh\HttpCache\Middleware\CacheRequests',

缓存现在已启用,针对公开响应。只需设置 Ttl 或 MaxSharedAge

Route::get('my-page', function(){
   return Response::make('Hello!')->setTtl(60); // Cache 1 minute
});

您还可以定义一个过滤器。

Route::filter('cache', function($route, $request, $response, $age=60){
    $response->setTtl($age);
});
Route::get('cached', array('after' => 'cache:30', function(){
    return 'I am cached 30 seconds!';
}));

发布配置以更改某些选项(缓存目录、默认 Ttl 等)或启用 ESI。

$ php artisan vendor:publish --provider="Barryvdh\HttpCache\ServiceProvider"

ESI

在您的配置文件中启用 ESI 并将 Esi Middleware 添加到您的 Kernel 中

'Barryvdh\HttpCache\Middleware\ParseEsi',

现在您可以在布局中定义 ESI 包含。

<esi:include src="<?= url('partial/page') ?>"/>

这将渲染部分/页面,具有它自己的 Ttl。页面其余部分将保持缓存(使用它自己的 Ttl)

清除/刷新缓存

您可以清除单个 URL 或仅删除整个缓存目录

App::make('http_cache.store')->purge($url);
\File::cleanDirectory(app('http_cache.cache_dir'));

或者使用 Artisan httpcache:clear 命令

$ php artisan httpcache:clear

更多信息

有关更多信息,请阅读 关于 Symfony HttpCache 的文档