slydeath / laravel-nested-caching
Nested Caching for Laravel with caches stack
Installs: 11 869
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/slydeath/laravel-nested-caching
Requires
- php: ^7.4|^8.1
- laravel/framework: ^10.10
README
Minimum requirements
- PHP 7.4
- Laravel 8
Installation
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
How to use?
Caching any HTML chunk
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
Model caching
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
Caching the model for a specified time
To cache the model for a certain time, specify the lifetime in minutes as the second parameter:
@cache($user, 1440) <div>...</div> @endCache
Updating the «parent»
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
Collection caching
Example of caching a collection:
@cache($users)
@foreach ($users as $user)
@include('user');
@endforeach
@endCache
How to remove stack cache?
Just run this code at bottom of your page:
app(SlyDeath\NestedCaching\CacheStack::class)->clearCache();
Workflow with another caches
How to collect keys?
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();
How to clear the stack of another caches?
Just callclearAnotherCaches method:
app(\SlyDeath\NestedCaching\CacheStack::class)->clearAnotherCaches();
How to save keys for different pages?
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();
How to clear the stack of another caches with custom cache key?
Just callclearAnotherCaches method and provide the key:
app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->clearAnotherCaches();
Enable PhpStorm support
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