Code Monkey home page Code Monkey logo

laravel-httpcache's Introduction

HttpCache for Laravel

Tests Packagist License Latest Stable Version Total Downloads Fruitcake

Laravel can use HttpKernelInterface Middlewares, so also HttpCache. This package provides a simple ServiceProvider to get you started with HttpCache.

First, require this package with composer

composer require barryvdh/laravel-httpcache

After updating, add the ServiceProvider to the array of providers in app/config/app.php

'Barryvdh\HttpCache\ServiceProvider',

You can now add the Middleware to your Kernel:

'Barryvdh\HttpCache\Middleware\CacheRequests',

Caching is now enabled, for public responses. Just set the Ttl or MaxSharedAge

Route::get('my-page', function(){
   return Response::make('Hello!')->setTtl(60); // Cache 1 minute
});

You can use the provided SetTtl middleware in your Kernel to simplify this:

protected $routeMiddleware = [
    // ...
    'ttl' => \Barryvdh\HttpCache\Middleware\SetTtl::class,
];

Route::get('my-page', function(){
   return 'Hello' 
})->middleware('ttl:60'); // Cache 1 minute

Publish the config to change some options (cache dir, default ttl, etc) or enable ESI.

$ php artisan vendor:publish --provider="Barryvdh\HttpCache\ServiceProvider"

Direct approach, without ServiceProvider

Note: This is still in beta, test with caution. It should be faster, but less flexible because it starts earlier.

You can also wrap the Kernel in the HttpCache, in your public/index.php. Replace the 'Run The Application' part like this:

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$kernel = \Barryvdh\HttpCache\CacheKernel::wrap($kernel);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

ESI

Enable ESI in your config file and add the Esi Middleware to your Kernel:

'Barryvdh\HttpCache\Middleware\ParseEsi',

You can now define ESI includes in your layouts.

<esi:include src="<?= url('partial/page') ?>"/>

This will render partial/page, with it's own TTL. The rest of the page will remain cached (using it's own TTL)

Purging/flushing the cache

You can purge a single url or just delete the entire cache directory:

App::make('http_cache.store')->purge($url);
\File::cleanDirectory(app('http_cache.cache_dir'));

Or use the Artisan httpcache:clear command

$ php artisan httpcache:clear

More information

For more information, read the Docs on Symfony HttpCache

laravel-httpcache's People

Contributors

anchepiece avatar barryvdh avatar fideloper avatar gonzahce avatar hannesvdvreken avatar hootlex avatar okaufmann avatar omranic avatar roennow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.