Skip to content

Latest commit

 

History

History
230 lines (174 loc) · 7.39 KB

README.md

File metadata and controls

230 lines (174 loc) · 7.39 KB

Speed up an app by caching the entire response

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

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

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

$ composer require spatie/laravel-responsecache

Next, you must install the service provider:

// config/app.php

'providers' => [
    ...
    Spatie\ResponseCache\ResponseCacheServiceProvider::class,
];

This package also comes with a facade.

// config/app.php

'aliases' => [
    ...
   'ResponseCache' => Spatie\ResponseCache\ResponseCacheFacade::class,
];

You can publish the config file with:

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

This is the contents of the published config file:

return [

    /**
     *  This is the master switch to enable of disable the response cache. If set to
     *  false no responses will be cached.
     */
    'enabled' => env('RESPONSE_CACHE_ENABLED', true),

    /**
     *  The given class will determinate if a request should be cached. The
     *  default class will cache all successful GET-requests.
     *
     *  You can provide your own class given that it implements the
     *  CacheProfile interface.
     */
    'cacheProfile' => Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests::class,

    /**
     * When using the default CacheRequestFilter this setting controls the
     * number of minutes responses must be cached.
     */
    'cacheLifetimeInMinutes' => env('RESPONSE_CACHE_LIFETIME', 60 * 24 * 7),

    /*
     * This setting determines if a http header named "Laravel-responsecache"
     * with the cache time should be added to a cached response. This
     * can be handy when debugging.
     */
    'addCacheTimeHeader' => env('APP_DEBUG', true),

    /*
     * Here you may define the cache store that should be used to store
     * requests. This can be the name of any store that is
     * configured in app/config/cache.php
     */
    'cacheStore' => env('RESPONSE_CACHE_DRIVER', 'file'),
];

Usage

###Basic usage

By default the package will cache all successful get-requests for a week. Logged in users will each have their own separate cache. If this behaviour is what you need, you're done: installing the ResponseCacheServerProvider was enough.

###Flushing the cache The entire cache can be flushed with:

ResponseCache::flush();

This will flush everything from the cache store specified in the config-file.

The same can be accomplished by issuing this artisan command:

$ php artisan responsecache:clear

###Preventing a request from being cached Requests can be ignored by using the doNotCacheResponse-middleware. This middleware [can be assigned to routes and controllers] (http://laravel.com/docs/master/controllers#controller-middleware).

Using the middleware are route could be exempt from being cached.

// app/Http/routes.php

Route::get('/auth/logout', ['middleware' => 'doNotCacheResponse', 'uses' => 'AuthController@getLogout']);

Alternatively you can add the middleware to a controller:

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

###Creating a custom cache profile To determine which requests should be cached, and for how long, a cache profile class is used. The default class that handles these questions is Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests.

You can create your own cache profile class by implementing the Spatie\ResponseCache\CacheProfiles\CacheProfile-interface. Let's take a look at the interface:

interface CacheProfile
{
    /**
     * Determine if the given request should be cached.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    public function shouldCacheRequest(Request $request);

    /**
     * Determine if the given response should be cached.
     *
     * @param \Symfony\Component\HttpFoundation\Response $response
     *
     * @return bool
     */
    public function shouldCacheResponse(Response $response);

    /**
     * Return the time when the cache must be invalidated.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \DateTime
     */
    public function cacheRequestUntil(Request $request);

    /**
     * Return a string to differentiate this request from others.
     *
     * For example: if you want a different cache per user you could return the id of
     * the logged in user.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return string
     */
    public function cacheNameSuffix(Request $request);
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

You can run the tests with:

$ composer test

Alternatives

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.