Skip to content

Commit 6bb8358

Browse files
committed
Initial commit
0 parents  commit 6bb8358

13 files changed

+735
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 SlyDeath
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Nested Caching for Laravel with caches stack
2+
3+
[![Latest Stable Version](https://poser.pugx.org/slydeath/laravel-nested-caching/v/stable)](https://packagist.org/packages/slydeath/laravel-nested-caching)
4+
[![Total Downloads](https://poser.pugx.org/slydeath/laravel-nested-caching/downloads)](https://packagist.org/packages/slydeath/laravel-nested-caching)
5+
[![License](https://poser.pugx.org/slydeath/laravel-nested-caching/license)](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

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "slydeath/laravel-nested-caching",
3+
"description": "Nested Caching for Laravel with caches stack",
4+
"license": "MIT",
5+
"version": "1.0",
6+
"authors": [
7+
{
8+
"name": "SlyDeath",
9+
"email": "slyrs@yandex.ru"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.4|^8.0",
14+
"laravel/framework": "^v8.0.0"
15+
},
16+
"minimum-stability": "dev",
17+
"autoload": {
18+
"psr-4": {
19+
"SlyDeath\\NestedCaching\\": "src/"
20+
}
21+
}
22+
}

config/nested-caching.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return [
4+
5+
// Force disable caching
6+
'disabled' => env('DISABLE_CACHING', false),
7+
8+
// Cache tag
9+
'cache-tag' => 'nested-caching',
10+
11+
// Environments where caching will be disabled
12+
'expelled-envs' => [
13+
'local',
14+
],
15+
];

src/BadDriverException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace SlyDeath\NestedCaching;
4+
5+
/**
6+
* Class BadDriverException
7+
*
8+
* @package SlyDeath\NestedCaching
9+
*/
10+
class BadDriverException extends \Exception
11+
{
12+
}

0 commit comments

Comments
 (0)