All Projects → spatie → Laravel Varnish

spatie / Laravel Varnish

Licence: mit
Making Varnish and Laravel play nice together

Projects that are alternatives of or similar to Laravel Varnish

Awesome Wp Speed Up
Plugins and resources to speed up and optimize your WordPress site.
Stars: ✭ 375 (+28.87%)
Mutual labels:  caching, varnish, performance
Laravel Responsecache
Speed up a Laravel app by caching the entire response
Stars: ✭ 1,874 (+543.99%)
Mutual labels:  laravel, varnish, performance
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (+28.87%)
Mutual labels:  caching, laravel
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (+1722.68%)
Mutual labels:  caching, performance
Rememberable
Query caching for Laravel
Stars: ✭ 960 (+229.9%)
Mutual labels:  caching, laravel
Meter
Laravel package to find performance bottlenecks in your laravel application.
Stars: ✭ 204 (-29.9%)
Mutual labels:  laravel, performance
Laravel Zero
A PHP framework for console artisans
Stars: ✭ 2,821 (+869.42%)
Mutual labels:  laravel, performance
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+45.7%)
Mutual labels:  caching, laravel
Efsecondlevelcache
Entity Framework 6.x Second Level Caching Library.
Stars: ✭ 63 (-78.35%)
Mutual labels:  caching, performance
Trickster
Open Source HTTP Reverse Proxy Cache and Time Series Dashboard Accelerator
Stars: ✭ 1,306 (+348.8%)
Mutual labels:  caching, performance
Laravel Model Caching
Eloquent model-caching made easy.
Stars: ✭ 1,829 (+528.52%)
Mutual labels:  caching, laravel
Laravel Partialcache
Blade directive to cache rendered partials in laravel
Stars: ✭ 205 (-29.55%)
Mutual labels:  laravel, performance
Laravel Mix Preload
Add preload and prefetch links based your Mix manifest
Stars: ✭ 162 (-44.33%)
Mutual labels:  laravel, performance
Foshttpcache
Integrate your PHP application with your HTTP caching proxy
Stars: ✭ 308 (+5.84%)
Mutual labels:  caching, varnish
Scaffeine
Thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine)
Stars: ✭ 195 (-32.99%)
Mutual labels:  caching, performance
Aimeos
Integrated online shop based on Laravel 8 and the Aimeos e-commerce framework
Stars: ✭ 2,354 (+708.93%)
Mutual labels:  laravel, performance
Laravel Image Optimizer
Optimize images in your Laravel app
Stars: ✭ 873 (+200%)
Mutual labels:  laravel, performance
Laravel Blink
Cache that expires in the blink of an eye
Stars: ✭ 114 (-60.82%)
Mutual labels:  laravel, performance
Ansible Role Memcached
Ansible Role - Memcached
Stars: ✭ 54 (-81.44%)
Mutual labels:  caching, performance
Pike
HTTP cache server, such as varnish
Stars: ✭ 155 (-46.74%)
Mutual labels:  caching, varnish

Making Varnish and Laravel play nice together

Latest Version on Packagist GitHub Workflow Status Total Downloads

This package provides an easy way to work with Varnish 4 (or 5) in Laravel. It provides a route middleware that, when applied to a route, will make sure Varnish will cache the response no matter what. The package also contains a function to flush the Varnish cache from within the application.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

We assume that you've already installed Varnish on your server. If not read this blogpost to learn how to install it.

You can install the package via composer:

composer require spatie/laravel-varnish

The package will automatically register itself for Laravel 5.5+.

If you are using Laravel < 5.5, you also need to add Varnish\VarnishServiceProvider to your config/app.php providers array:

\Spatie\Varnish\VarnishServiceProvider::class

Next if you use Laravel you must publish the config-file with:

php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config"

and if you use Lumen, you must copy config/varnish.php file to your application config folder.

This is the contents of the published file:

return [
    /*
     * The hostname this Laravel app is listening to.
     */
    'host' => 'example.com',

    /*
     * The location of the file containing the administrative password.
     */
    'administrative_secret' => '/etc/varnish/secret',

    /*
     * The port where the administrative tasks may be sent to.
     */
    'administrative_port' => 6082,

    /*
     * The default amount of minutes that content rendered using the `CacheWithVarnish`
     * middleware should be cached.
     */
    'cache_time_in_minutes' => 60 * 24,

    /*
     * The name of the header that triggers Varnish to cache the response.
     */
    'cacheable_header_name' => 'X-Cacheable',
];

In the published varnish.php config file you should set the host key to the right value.

Add the Spatie\Varnish\Middleware\CacheWithVarnish middleware to the route middlewares.

For Laravel:

// app/Http/Kernel.php
protected $routeMiddleware = [
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
];

If you are using Lumen, you need to load config file before route middleware definition to your bootstrap/app.php:

$app->configure('varnish');
$app->routeMiddleware([
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
]);

Finally, you should add these lines to the vcl_backend_response function in your VCL (by default this is located at /etc/varnish/default.vcl on your server):

if (beresp.http.X-Cacheable ~ "1") {
    unset beresp.http.set-cookie;
}

We highly recommend using the VCL provided the varnish-5.0-configuration-templates repo made by Mattias Geniar.

Usage

Caching responses

The routes whose response should be cached should use the cacheable middleware.

// your routes file

//will be cached by Varnish
Route::group(['middleware' => 'cacheable'], function() {
    Route::get('/', '[email protected]');
    Route::get('/contact', '[email protected]');
});

//won't be cached by Varnish
Route::get('do-not-cache', '[email protected]');

The amount of minutes that Varnish should cache this content can be configured in the cache_time_in_minutes key in the laravel-varnish.php config file. Alternatively you could also use a middleware parameter to specify that value.

// Varnish will cache the responses of the routes inside the group for 15 minutes
Route::group(['middleware' => 'cacheable:15'], function() {
   ...
});

Behind the scenes the middleware will add an X-Cacheable and Cache-Control to the response. Varnish will remove all cookies from Laravel's response. So keep in mind that, because thelaravel_session cookie will be removed as well, sessions will not work on routes were the CacheWithVarnish middleware is applied.

Clearing cache from Varnish

There's an artisan command to flush the cache. This can come in handy in your deployment script.

php artisan varnish:flush

Under the hood flushing the cache will call the sudo varnishadm. To make it work without any hassle make sure the command is run by a unix user that has sudo rights.

You can also do this in your code to flush the cache:

(new Spatie\Varnish\Varnish())->flush();

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

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

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].