/laravel-partialcache

Blade directive to cache rendered partials in laravel

Primary LanguagePHPMIT LicenseMIT

Laravel Cache Partial Blade Directive

Latest Version on Packagist Software License SensioLabsInsight Quality Score Total Downloads

This package provides a Blade directive for Laravel 5.1 to cache rendered partials in laravel.

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

Install

You can install the package via Composer:

$ composer require spatie/laravel-partialcache

Start by registering the package's the service provider and facade:

// config/app.php

'providers' => [
  ...
  'Spatie\PartialCache\PartialCacheServiceProvider',
],

'aliases' => [
  ...
  'PartialCache' => 'Spatie\PartialCache\PartialCacheFacade',
],

The facade is optional, but the rest of this guide assumes you're using it.

Optionally publish the config files:

$ php artisan vendor:publish --provider="Spatie\PartialCache\PartialCacheServiceProvider"

Usage

The package registers a blade directive, @cache. The cache directive accepts the same arguments as @include, plus optional parameters for the amount of minutes a view should be cached for, a key unique to the rendered view, and a cache tag for the rendered view. If no minutes are provided, the view will be remembered until you manually remove it from the cache.

Note that this caches the rendered html, not the rendered php like blade's default view caching.

{{-- Simple example --}}
@cache('footer.section.partial')

{{-- With extra view data --}}
@cache('products.card', ['product' => $category->products->first()])

{{-- For a certain time --}}
{{-- (cache will invalidate in 60 minutes in this example, set null to remember forever) --}}
@cache('homepage.news', null, 60)

{{-- With an added key (cache entry will be partialcache.user.profile.{$user->id}) --}}
@cache('user.profile', null, null, $user->id)

{{-- With an added tag (only supported by memcached and others) }}
@cache('user.profile', null, null, $user->id, 'userprofiles')

Clearing The PartialCache

You can forget a partialcache entry with PartialCache::forget($view, $key).

PartialCache::forget('user.profile', $user->id);

If you want to flush all entries, you'll need to either call PartialCache::flush() (note: this is only supported by drivers that support tags), or clear your entire cache.

Configuration

Configuration isn't necessary, but there are two options specified in the config file:

  • partialcache.enabled: Fully enable or disable the cache. Defaults to true.
  • partialcache.directive: The name of the blade directive to register. Defaults to cache.
  • partialcache.key: The base key that used for cache entries. Defaults to partialcache.

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be 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.