Skip to content

Commit fb5e584

Browse files
committed
Fixes and improvements:
- Removes addiction to `Redis` and `Memcached` - Adds the ability to collect all cache keys and clear them
1 parent 340edc9 commit fb5e584

File tree

8 files changed

+184
-54
lines changed

8 files changed

+184
-54
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "slydeath/laravel-nested-caching",
33
"description": "Nested Caching for Laravel with caches stack",
44
"license": "MIT",
5-
"version": "1.1",
5+
"version": "1.2",
66
"authors": [
77
{
88
"name": "SlyDeath",
@@ -16,7 +16,8 @@
1616
"minimum-stability": "dev",
1717
"autoload": {
1818
"psr-4": {
19-
"SlyDeath\\NestedCaching\\": "src/"
19+
"SlyDeath\\NestedCaching\\": "src/",
20+
"SlyDeath\\NestedCaching\\Providers\\": "src/Providers"
2021
}
2122
}
2223
}

config/nested-caching.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
return [
44

55
// Force disable caching
6-
'disabled' => env('DISABLE_CACHING', false),
7-
8-
// Cache tag
9-
'cache-tag' => 'nested-caching',
6+
'disabled' => env('DISABLE_CACHING', false),
107

118
// Environments where caching will be disabled
12-
'expelled-envs' => [
9+
'expelled-envs' => [
1310
'local',
1411
],
12+
13+
// Use another caching?
14+
'another-caching' => env('ENABLE_ANOTHER_CACHING', true),
15+
16+
// Cache tag
17+
'cache-tag' => 'nested-caching',
1518
];

src/BadDriverException.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/CacheStack.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ class CacheStack
1616
*/
1717
protected array $keys = [];
1818

19+
/**
20+
* List of another caches
21+
*
22+
* @var array $another_caches
23+
*/
24+
protected array $another_caches = [];
25+
26+
/**
27+
* Default key for storing another caches
28+
*
29+
* @var string
30+
*/
31+
protected string $another_caches_key = 'slydeath:nc:ac';
32+
1933
/**
2034
* Cache instance
2135
*
@@ -38,9 +52,53 @@ public function __construct(Caching $caching)
3852
*
3953
* @param string $key Caching key
4054
*/
41-
public function setKey(string $key)
55+
public function setKey(string $key): CacheStack
4256
{
4357
$this->keys[] = $key;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* Add another key
64+
*
65+
* @param string $key Caching key
66+
*/
67+
public function setAnotherKey(string $key): CacheStack
68+
{
69+
$this->another_caches_key = $key;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* Get a list of keys
76+
*
77+
* @return array
78+
*/
79+
public function getKeys(): array
80+
{
81+
return $this->keys;
82+
}
83+
84+
/**
85+
* Collect another caches
86+
*
87+
* @param array $data Another cache data
88+
*/
89+
public function addAnotherCache(array $data)
90+
{
91+
$this->another_caches[] = $data;
92+
}
93+
94+
/**
95+
* Get list of another caches
96+
*
97+
* @return mixed
98+
*/
99+
public function getAnotherCaches()
100+
{
101+
return \Cache::rememberForever($this->another_caches_key, fn() => $this->another_caches);
44102
}
45103

46104
/**
@@ -62,12 +120,19 @@ public function clearCache($keys = null): bool
62120
}
63121

64122
/**
65-
* Get a list of keys
66-
*
67-
* @return array
123+
* Clear another caches
68124
*/
69-
public function getKeys(): array
125+
public function clearAnotherCaches()
70126
{
71-
return $this->keys;
127+
foreach ($this->getAnotherCaches() as $cache) {
128+
$tags = data_get($cache, 'tags');
129+
$key = data_get($cache, 'key');
130+
131+
if (count($tags)) {
132+
\Cache::tags($tags)->forget($key);
133+
} else {
134+
\Cache::forget($key);
135+
}
136+
}
72137
}
73138
}

src/CacheWrittenListener.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace SlyDeath\NestedCaching;
4+
5+
use Illuminate\Cache\Events\KeyWritten;
6+
7+
/**
8+
* Class CacheWrittenListener
9+
*
10+
* @package SlyDeath\NestedCaching
11+
*/
12+
class CacheWrittenListener
13+
{
14+
/**
15+
* Handle the event
16+
*
17+
* @param KeyWritten $event
18+
*
19+
* @return void
20+
*/
21+
public function handle(KeyWritten $event)
22+
{
23+
if (config('nested-caching.another-caching')) {
24+
app(CacheStack::class)->addAnotherCache([
25+
'tags' => $event->tags,
26+
'key' => $event->key,
27+
]);
28+
}
29+
}
30+
}

src/Caching.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Caching
2525
*/
2626
public function __construct(Cache $cache)
2727
{
28-
$this->cache = $cache->tags(config('nested-caching.cache-tag'));
28+
$this->cache = $cache->supportsTags() ? $cache->tags(config('nested-caching.cache-tag')) : $cache;
2929
}
3030

3131
/**

src/NestedCachingServiceProvider.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Blade;
66
use Illuminate\Contracts\Http\Kernel;
77
use Illuminate\Support\ServiceProvider;
8+
use SlyDeath\NestedCaching\Providers\EventServiceProvider;
89

910
/**
1011
* Class NestedCachingServiceProvider
@@ -13,46 +14,18 @@
1314
*/
1415
class NestedCachingServiceProvider extends ServiceProvider
1516
{
16-
/**
17-
* Supported drivers
18-
*
19-
* @var array
20-
*/
21-
protected $supported_drivers = [
22-
'redis',
23-
'memcached',
24-
];
25-
2617
/**
2718
* Bootstrap any application services
2819
*
2920
* @param Kernel $kernel
30-
*
31-
* @throws BadDriverException
3221
*/
3322
public function boot(Kernel $kernel)
3423
{
35-
$this->checkCacheDriverSupport();
3624
$this->applyMiddleware($kernel);
3725
$this->applyBladeDirectives();
3826
$this->publishConfig();
3927
}
4028

41-
/**
42-
* Checks cache driver for compatibility
43-
*
44-
* @throws BadDriverException
45-
*/
46-
public function checkCacheDriverSupport()
47-
{
48-
if ( ! in_array(config('cache.default'), $this->supported_drivers, true)) {
49-
throw new BadDriverException(
50-
'Your cache driver does not supported.
51-
Supported drivers: ' . implode(', ', $this->supported_drivers)
52-
);
53-
}
54-
}
55-
5629
/**
5730
* Installing middleware to entire clear the cache
5831
*
@@ -109,6 +82,8 @@ public function register()
10982
$config_path = __DIR__ . '/../config/nested-caching.php';
11083
$this->mergeConfigFrom($config_path, 'nested-caching');
11184

85+
$this->app->register(EventServiceProvider::class);
86+
11287
$this->app->singleton(CacheStack::class);
11388
$this->app->singleton(BladeDirectives::class);
11489
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace SlyDeath\NestedCaching\Providers;
4+
5+
use Illuminate\Cache\Events\KeyWritten;
6+
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\ServiceProvider;
8+
use SlyDeath\NestedCaching\CacheWrittenListener;
9+
10+
/**
11+
* Class EventServiceProvider
12+
*
13+
* @package SlyDeath\NestedCaching\Providers
14+
*/
15+
class EventServiceProvider extends ServiceProvider
16+
{
17+
/**
18+
* The event listener mappings for the package
19+
*
20+
* @var array
21+
*/
22+
protected array $listen = [
23+
KeyWritten::class => [
24+
CacheWrittenListener::class,
25+
],
26+
];
27+
28+
/**
29+
* The subscriber classes to register
30+
*
31+
* @var array
32+
*/
33+
protected array $subscribe = [];
34+
35+
/**
36+
* Get the events and handlers
37+
*
38+
* @return array
39+
*/
40+
public function listens(): array
41+
{
42+
return $this->listen;
43+
}
44+
45+
/**
46+
* Register the package's event listeners
47+
*/
48+
public function boot()
49+
{
50+
foreach ($this->listens() as $event => $listeners) {
51+
foreach ($listeners as $listener) {
52+
Event::listen($event, $listener);
53+
}
54+
}
55+
56+
foreach ($this->subscribe as $subscriber) {
57+
Event::subscribe($subscriber);
58+
}
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function register()
65+
{
66+
//
67+
}
68+
}

0 commit comments

Comments
 (0)