2017 © Pedro Peláez
 

library cache

Laravel 4 Route Caching

image

titzu/cache

Laravel 4 Route Caching

  • Monday, November 4, 2013
  • by titzu
  • Repository
  • 4 Watchers
  • 10 Stars
  • 104 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

This is a Laravel 4 package that provides caching for the response of the application., (*1)

Installation

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

"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:, (*3)

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., (*4)

'Titzu\RouterCache\RouterCacheServiceProvider'

That's all!, (*5)

Usage

Register route for caching

You must register the filter with the route, (*6)

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

Of course you can also register a group of routes, (*7)

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., (*8)

Note

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

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

and, (*10)

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

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

Contact

Feel free to use, fork, whatever :), (*12)

The Versions

04/11 2013

dev-master

9999999-dev

Laravel 4 Route Caching

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

by Catalin Ioan Titu

laravel cache route response router