• Stars
    star
    459
  • Rank 95,377 (Top 2 %)
  • Language
    PHP
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel HTTP Cache

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

More Repositories

1

laravel-debugbar

Debugbar for Laravel (Integrates PHP Debug Bar)
PHP
15,498
star
2

laravel-ide-helper

IDE Helper for Laravel
PHP
13,496
star
3

laravel-dompdf

A DOMPDF Wrapper for Laravel
PHP
6,091
star
4

laravel-snappy

Laravel Snappy PDF
PHP
2,447
star
5

laravel-translation-manager

Manage Laravel translation files
PHP
1,564
star
6

laravel-elfinder

elFinder bundle for Laravel
PHP
730
star
7

laravel-async-queue

Laravel Async Queue Driver
PHP
290
star
8

laravel-migration-generator

Generate migrations based on existing tables
PHP
246
star
9

elfinder-flysystem-driver

elFinder driver for Flysystem
PHP
182
star
10

laravel-omnipay

Omnipay ServiceProvider for Laravel
PHP
169
star
11

laravel-vendor-cleanup

L4 Vendor Cleanup Command (DEPRECATED!)
PHP
162
star
12

composer-cleanup-plugin

Composer plugin for cleaning up unused files from packages.
PHP
143
star
13

laravel-form-bridge

Laravel Bridge for the Symfony Form Component
PHP
143
star
14

laravel-twigbridge

Laravel TwigBridge
PHP
59
star
15

laravel-security

Laravel Security
PHP
53
star
16

laravel-stack-middleware

Stack Middleware for Laravel
PHP
40
star
17

laravel-assetic

Assetic ServiceProvider for Laravel
PHP
37
star
18

barryvdh.github.io

Jekyll blog for barryvdh.nl
HTML
16
star
19

OAuth

PSR2 compliant OAuth 1.0 library based on Andy Smith's OAuth library here: http://oauth.googlecode.com/svn/code/php/
PHP
6
star
20

picofeed

Picofeed fork
PHP
4
star
21

elfinder-builds

Mirror from the nightly builds from elFinder
JavaScript
3
star
22

assetic-filters

Additional CSSminFilter and UriPrependFilter, using the mrclay/minify libraries
PHP
3
star