• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

barryvdh/laravel-httpcache: Laravel HTTP Cache

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

barryvdh/laravel-httpcache

开源软件地址(OpenSource Url):

https://github.com/barryvdh/laravel-httpcache

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

HttpCache for Laravel

Tests Packagist License Latest Stable Version Total Downloads Fruitcake

Laravel can use HttpKernelInterface Middlewares, so also HttpCache. This package provides a simple ServiceProvider to get you started with HttpCache.

First, require this package with composer

composer require barryvdh/laravel-httpcache

After updating, add the ServiceProvider to the array of providers in app/config/app.php

'Barryvdh\HttpCache\ServiceProvider',

You can now add the Middleware to your Kernel:

'Barryvdh\HttpCache\Middleware\CacheRequests',

Caching is now enabled, for public responses. Just set the Ttl or MaxSharedAge

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

You can use the provided SetTtl middleware in your Kernel to simplify this:

protected $routeMiddleware = [
    // ...
    'ttl' => \Barryvdh\HttpCache\Middleware\SetTtl::class,
];

Route::get('my-page', function(){
   return 'Hello' 
})->middleware('ttl:60'); // Cache 1 minute

Publish the config to change some options (cache dir, default ttl, etc) or enable ESI.

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

Direct approach, without ServiceProvider

Note: This is still in beta, test with caution. It should be faster, but less flexible because it starts earlier.

You can also wrap the Kernel in the HttpCache, in your public/index.php. Replace the 'Run The Application' part like this:

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$kernel = \Barryvdh\HttpCache\CacheKernel::wrap($kernel);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

ESI

Enable ESI in your config file and add the Esi Middleware to your Kernel:

'Barryvdh\HttpCache\Middleware\ParseEsi',

You can now define ESI includes in your layouts.

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

This will render partial/page, with it's own TTL. The rest of the page will remain cached (using it's own TTL)

Purging/flushing the cache

You can purge a single url or just delete the entire cache directory:

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

Or use the Artisan httpcache:clear command

$ php artisan httpcache:clear

More information

For more information, read the Docs on Symfony HttpCache




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap