|
| 1 | +# Nested Caching for Laravel with caches stack |
| 2 | + |
| 3 | +[](https://packagist.org/packages/slydeath/laravel-nested-caching) |
| 4 | +[](https://packagist.org/packages/slydeath/laravel-nested-caching) |
| 5 | +[](https://packagist.org/packages/slydeath/laravel-nested-caching) |
| 6 | + |
| 7 | +## Minimum requirements |
| 8 | + |
| 9 | +- PHP 7.4 |
| 10 | +- Laravel 8 |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Add package to composer.json: |
| 15 | + |
| 16 | +```bash |
| 17 | +composer require slydeath/laravel-nested-caching |
| 18 | +``` |
| 19 | + |
| 20 | +Open `config/app.php` and add the service provider to the array `providers`: |
| 21 | + |
| 22 | +```php |
| 23 | +SlyDeath\NestedCaching\NestedCachingServiceProvider::class, |
| 24 | +``` |
| 25 | + |
| 26 | +To place the configuration file, run: |
| 27 | + |
| 28 | +```bash |
| 29 | +php artisan vendor:publish --provider="SlyDeath\NestedCaching\NestedCachingServiceProvider" --tag=config |
| 30 | +``` |
| 31 | + |
| 32 | +## How to use? |
| 33 | + |
| 34 | +### Caching any HTML chunk |
| 35 | + |
| 36 | +To cache any HTML chunk, you just need to pass the caching key to the `@cache` directive fragment: |
| 37 | + |
| 38 | +```html |
| 39 | +@cache('simple-cache') |
| 40 | + <div> |
| 41 | + This is an arbitrary piece of HTML that will be cached |
| 42 | + using the «simple-cache» key |
| 43 | + </div> |
| 44 | +@endCache |
| 45 | +``` |
| 46 | + |
| 47 | +### Model caching |
| 48 | + |
| 49 | +To enable model caching support, add the trait to it `NestedCacheable`: |
| 50 | + |
| 51 | +```php |
| 52 | +use SlyDeath\NestedCaching\NestedCacheable; |
| 53 | + |
| 54 | +class User extends Model |
| 55 | +{ |
| 56 | + use NestedCacheable; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +In the template, to cache a model, you need to pass its instance to the `@cache` directive: |
| 61 | + |
| 62 | +```html |
| 63 | +@cache($user) |
| 64 | +<div>App\User model caching:</div> |
| 65 | +<ul> |
| 66 | + <li>Name: {{ $user->name }}</li> |
| 67 | + <li>Email: {{ $user->email }}</li> |
| 68 | +</ul> |
| 69 | +@endCache |
| 70 | +``` |
| 71 | + |
| 72 | +### Caching the model for a specified time |
| 73 | + |
| 74 | +To cache the model for a certain time, specify the lifetime in minutes as the second parameter: |
| 75 | + |
| 76 | +```html |
| 77 | +@cache($user, 1440) |
| 78 | + <div>...</div> |
| 79 | +@endCache |
| 80 | + ``` |
| 81 | + |
| 82 | +#### Updating the «parent» |
| 83 | + |
| 84 | +For update the cache of the «parent model», we need setup touches: |
| 85 | + |
| 86 | +```php |
| 87 | +use SlyDeath\NestedCaching\NestedCacheable; |
| 88 | + |
| 89 | +class CarUser extends Model |
| 90 | +{ |
| 91 | + use NestedCacheable; |
| 92 | + |
| 93 | + // Specifying the parent relations |
| 94 | + protected $touches = ['user']; |
| 95 | + |
| 96 | + // Parent relation |
| 97 | + public function user() |
| 98 | + { |
| 99 | + return $this->belongsTo(User::class); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Usage example: |
| 105 | + |
| 106 | +**resources/views/user.blade.php** |
| 107 | + |
| 108 | +```html |
| 109 | +@cache($user) |
| 110 | +<section> |
| 111 | + <h2>User's cars {{ $user->name }}</h2> |
| 112 | + <ul> |
| 113 | + @foreach($user->cars as $car) |
| 114 | + @include('user-car'); |
| 115 | + @endforeach |
| 116 | + </ul> |
| 117 | +</section> |
| 118 | +@endCache |
| 119 | +``` |
| 120 | + |
| 121 | +**resources/views/user-car.blade.php** |
| 122 | + |
| 123 | +```html |
| 124 | +@cache($car) |
| 125 | + <li>{{ $car->brand }}</li> |
| 126 | +@endCache |
| 127 | +``` |
| 128 | + |
| 129 | +### Collection caching |
| 130 | + |
| 131 | +Example of caching a collection: |
| 132 | + |
| 133 | +```html |
| 134 | +@cache($users) |
| 135 | + @foreach ($users as $user) |
| 136 | + @include('user'); |
| 137 | + @endforeach |
| 138 | +@endCache |
| 139 | +``` |
| 140 | + |
| 141 | +### How to remove stack cache? |
| 142 | + |
| 143 | +Just run this code at bottom of your page: |
| 144 | + |
| 145 | +```php |
| 146 | +app(SlyDeath\NestedCaching\CacheStack::class)->clearCache(); |
| 147 | +``` |
| 148 | + |
| 149 | +## Enable PhpStorm support |
| 150 | + |
| 151 | +Go to the `Settings → PHP → Blade`, then uncheck **Use default settings**. Go to **Directives** tab and press «+» to add |
| 152 | +another one custom directive: |
| 153 | + |
| 154 | +- Name: `cache` |
| 155 | +- Checkbox **Has parameters** → `true` |
| 156 | +- Prefix: ```<?php if ( ! app('SlyDeath\NestedCaching\BladeDirectives')->cache(``` |
| 157 | +- Suffix: ```)) { ?>``` |
| 158 | + |
| 159 | +And add close directive without parameters: |
| 160 | + |
| 161 | +- Name: `endcache` or `endCache`, whatever you use |
0 commit comments