7lab/laravel-responsecache

This package is abandoned and no longer maintained. The author suggests using the sevenlab/laravel-responsecache package instead.

Cache responses in a Laravel application

2.2.0 2024-10-30 09:56 UTC

This package is auto-updated.

Last update: 2024-10-30 09:58:17 UTC


README

Latest Version on Packagist Software License Total Downloads

Cache responses

This Laravel package can cache an entire response. By default it will cache all successful GET-requests for a week. This could potentially speed up the response quite considerably.

So the first time a request comes in the package will save the response before sending it to the users. When the same request comes in again we're not going through the entire application but just respond with the saved response.

The package is based on spatie/laravel-responsecache but uses the defined route names so the cached responses can easily be cleared without having to clear the whole cache.

Installation

You can install the package via Composer:

composer require sevenlab/laravel-responsecache

The package will automatically register itself.

You can publish the config file with:

php artisan vendor:publish --provider="SevenLab\ResponseCache\ResponseCacheServiceProvider"

This is the contents of the published config file (config/responsecache.php):

return [

    /*
     * Determine if the response cache middleware should be enabled.
     */
    'enabled' => env('RESPONSECACHE_ENABLED', true),

    /*
     * Specify the tag name that will be used for the cache.
     */
    'tag' => env('RESPONSECACHE_TAG', 'responsecache'),

];

And finally you should install the provided middlewares in the HTTP kernel (app/Http/Kernel.php).

...

protected $routeMiddleware = [
    ...
    'cacheResponse' => \SevenLab\ResponseCache\Middleware\CacheResponse::class,
    'doNotCacheResponse' => \SevenLab\ResponseCache\Middleware\DoNotCacheResponse::class,
];

...

Usage

By default it will cache all successful GET-requests for a week. Logged in users will each have their own separate cache.

Caching specific routes

When using the route middleware you can specify the number of minutes these routes should be cached:

// cache this route for 5 minutes
Route::get('/my-special-snowflake', 'SnowflakeController@index')->middleware('cacheResponse:5');

// cache all these routes for 10 minutes
Route::group(function() {
   Route::get('/another-special-snowflake', 'AnotherSnowflakeController@index');
   
   Route::get('/yet-another-special-snowflake', 'YetAnotherSnowflakeController@index');
})->middleware('cacheResponse:10');

Preventing a route from being cached

Requests can be ignored by using the doNotCacheResponse-middleware. This middleware can be assigned to routes and controllers.

Using the middleware on a route:

Route::get('/auth/logout', 'AuthController@getLogout')->name('auth.logout')->middleware('doNotCacheResponse');

Alternatively you can add the middleware to a controller:

class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('doNotCacheResponse', ['only' => ['getLogout']]);
    }
}

Clearing specific routes

Specific routes can be cleared with:

ResponseCache::forget(['auth.logout']);

The same can be accomplished by issuing this artisan command:

php artisan responsecache:forget auth.logout

Clearing all routes

The entire cache can be cleared with:

ResponseCache::clear();

This will clear everything from the cache store specified in the config-file (config/cache.php) with the tag specified in the responsecache-file (config/responsecache.php).

The same can be accomplished by issuing this artisan command:

php artisan responsecache:clear

Credits