- PHP 7.4
- Laravel 8
Add package to composer.json:
composer require slydeath/laravel-nested-caching
Open config/app.php
and add the service provider to the array providers
:
SlyDeath\NestedCaching\NestedCachingServiceProvider::class,
To place the configuration file, run:
php artisan vendor:publish --provider="SlyDeath\NestedCaching\NestedCachingServiceProvider" --tag=config
To cache any HTML chunk, you just need to pass the caching key to the @cache
directive fragment:
@cache('simple-cache')
<div>
This is an arbitrary piece of HTML that will be cached
using the «simple-cache» key
</div>
@endCache
To enable model caching support, add the trait to it NestedCacheable
:
use SlyDeath\NestedCaching\NestedCacheable;
class User extends Model
{
use NestedCacheable;
}
In the template, to cache a model, you need to pass its instance to the @cache
directive:
@cache($user)
<div>App\User model caching:</div>
<ul>
<li>Name: {{ $user->name }}</li>
<li>Email: {{ $user->email }}</li>
</ul>
@endCache
To cache the model for a certain time, specify the lifetime in minutes as the second parameter:
@cache($user, 1440)
<div>...</div>
@endCache
For update the cache of the «parent model», we need setup touches:
use SlyDeath\NestedCaching\NestedCacheable;
class CarUser extends Model
{
use NestedCacheable;
// Specifying the parent relations
protected $touches = ['user'];
// Parent relation
public function user()
{
return $this->belongsTo(User::class);
}
}
Usage example:
resources/views/user.blade.php
@cache($user)
<section>
<h2>User's cars {{ $user->name }}</h2>
<ul>
@foreach($user->cars as $car)
@include('user-car');
@endforeach
</ul>
</section>
@endCache
resources/views/user-car.blade.php
@cache($car)
<li>{{ $car->brand }}</li>
@endCache
Example of caching a collection:
@cache($users)
@foreach ($users as $user)
@include('user');
@endforeach
@endCache
Just run this code at bottom of your page:
app(SlyDeath\NestedCaching\CacheStack::class)->clearCache();
Keys are automatically collected in SlyDeath\NestedCaching\CacheWrittenListener
if another-caching
is enabled,
but you must save them manually (at the end of executing app) if you want to use them elsewhere:
app(\SlyDeath\NestedCaching\CacheStack::class)->getAnotherCaches();
Just callclearAnotherCaches
method:
app(\SlyDeath\NestedCaching\CacheStack::class)->clearAnotherCaches();
You can generate dynamically the cache key (for example from the page id):
app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->getAnotherCaches();
Just callclearAnotherCaches
method and provide the key:
app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->clearAnotherCaches();
Go to the Settings → PHP → Blade
, then uncheck Use default settings. Go to Directives tab and press «+» to add
another one custom directive:
- Name:
cache
- Checkbox Has parameters →
true
- Prefix:
<?php if ( ! app('SlyDeath\NestedCaching\BladeDirectives')->cache(
- Suffix:
)) { ?>
And add close directive without parameters:
- Name:
endcache
orendCache
, whatever you use