- PHP 7.4
- Laravel 8
Add package to composer.json:
composer require slydeath/laravel-nested-cachingOpen 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=configTo 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>
@endCacheTo 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>
@endCacheTo cache the model for a certain time, specify the lifetime in minutes as the second parameter:
@cache($user, 1440)
<div>...</div>
@endCacheFor 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>
@endCacheresources/views/user-car.blade.php
@cache($car)
<li>{{ $car->brand }}</li>
@endCacheExample of caching a collection:
@cache($users)
@foreach ($users as $user)
@include('user');
@endforeach
@endCacheJust 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:
endcacheorendCache, whatever you use