slydeath/laravel-nested-caching

Nested Caching for Laravel with caches stack
11,869 1
Install
composer require slydeath/laravel-nested-caching
Latest Version:3.0
PHP:^7.4|^8.1
Laravel:10
Older releases:8 · 9
License:MIT
Last Updated:Jun 20, 2023
Links: GitHub  ·  Packagist
Maintainer: SlyDeath

Nested Caching for Laravel with caches stack

Latest Stable Version Total Downloads License

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: endcache or endCache, whatever you use

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

644,549,754 9,698
laravel/framework

The Laravel Framework.

598,760,099 34,966
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

403,217,078 4,664

Version History

Version Released PHP Laravel License
3.0 ^7.4|^8.1 ^10.10 MIT
2.0 ^7.4|^8.1 ^v9.19 MIT
1.3 ^7.4|^8.0 ^v8.0.0 MIT
1.2 ^7.4|^8.0 ^v8.0.0 MIT
1.1 ^7.4|^8.0 ^v8.0.0 MIT