New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

shawnley/laravel-httpcache

HttpCache for Laravel 5
741
Install
composer require shawnley/laravel-httpcache
Latest Version:0.2.5
PHP:>=5.4.0
Laravel:5
Older releases:4
License:MIT
Last Updated:Jun 29, 2015
Links: GitHub  ·  Packagist
Maintainer: Shawnley

HttpCache for Laravel 5

For Laravel 4.1+, require v0.1.x

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

First, require this package in composer.json and run composer update

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

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 also define a filter.

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!';
}));

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

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

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

Related Packages

barryvdh/laravel-httpcache

HttpCache for Laravel

414,412 502
bunkuris/has-cache

HasCache trait for Laravel models with cache key managers

3,596 0
silber/page-cache

Caches responses as static files on disk for lightning fast page loads.

526,472 1,262
laracasts/matryoshka

Russian Doll Caching for Laravel.

60,992 237
geekcache/geekcache

A wrapper for Memcached with tags, soft invalidation, memoization, and regenerat...

38,888 6

Version History

Version Released PHP Laravel License
0.2.5 >=5.4.0 5.0.x|5.1.x MIT
0.2.4 >=5.4.0 5.0.x|5.1.x MIT
0.2.3 >=5.4.0 5.0.x|5.1.x MIT
v0.2.2 >=5.4.0 5.0.x|5.1.x MIT
v0.2.1 >=5.4.0 5.0.x|5.1.x MIT