New

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

titzu/cache

Laravel 4 Route Caching
115 11
Install
composer require titzu/cache:dev-master
PHP:>=5.3.0
Last Updated:Nov 4, 2013
Links: GitHub  ·  Packagist
Maintainer: titzu

This is a Laravel 4 package that provides caching for the response of the application.

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require titzu/cache.

"require": {
        "laravel/framework": "4.0.*",
        "titzu/cache": "dev-master"
},
"require-dev": {
        "phpunit/phpunit": "3.7.*",
        "mockery/mockery": "dev-master@dev"
},
"minimum-stability" : "dev"

Next, update Composer from the Terminal:

composer update

Once this operation completes, the final step is to add the service provider. Open app/config/app.php, and add a new item to the providers array.

'Titzu\RouterCache\RouterCacheServiceProvider'

That's all!

Usage

Register route for caching

You must register the filter with the route

Route::get('/', array('before' => 'cache_get', 'after' => 'cache_set', 'uses' => 'HomeController@show'));

Of course you can also register a group of routes

Route::group(array('before' => 'cache_get', 'after' => 'cache_set'), function()
{
    Route::get('/', 'HomeController@show');
    Route::get('another', 'AnotherController@show');
});

Clear Routes Cache

php artisan router:clear

By default Laravel uses file cache driver for cache storage. Because cache sections are not supported when using the file or database cache drivers and in order not to flush all cache when we need to refresh just the routes cache I keep cached routes in a separate cache entry 'cached_routes'. The router:clear command will then only operate on the keys registered in cached_routes array.

Note

In order to verify that the request is served from the cache or on the fly, I added a new header entry

$response->header('Served-From', 'cache');

and

$response->header('Served-From', 'laravel');

You can then find this using entry developer tools ( firebug for example ).

Contact

Feel free to use, fork, whatever :)

Related Packages

heropoo/routing

A simple and fast route. Use tree structure storage, fast matching!

5,672 10
fire015/json-routes

Define your routes in laravel using JSON files

18 3
spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

10,581,913 2,825
maartenstaa/laravel-41-route-caching

This package allows you to cache your routes definitions, thereby speeding up ea...

389,397 25
izniburak/laravel-auto-routes

Auto Route Generating (Auto-Discovery) Package for Laravel

55,875 234