From 46466323a6f01adab86d22fd7872320c07d76ebf Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 15 Jan 2024 11:00:21 +0300 Subject: [PATCH] Added the ability to work with tags without keys --- README.md | 14 +++++++++++++- src/Services/Cache.php | 7 +++++++ src/Services/Storages/Disabled.php | 2 ++ src/Services/Storages/MainStore.php | 5 +++++ src/Services/Storages/TaggedStore.php | 5 +++++ src/Support/CacheManager.php | 5 +++++ tests/Cache/NotWhen/Simple/ArrayTest.php | 17 +++++++++++++++++ tests/Cache/NotWhen/Simple/FileTest.php | 17 +++++++++++++++++ tests/Cache/NotWhen/Simple/RedisTest.php | 17 +++++++++++++++++ tests/Cache/When/Simple/ArrayTest.php | 17 +++++++++++++++++ tests/Cache/When/Simple/FileTest.php | 17 +++++++++++++++++ tests/Cache/When/Simple/RedisTest.php | 17 +++++++++++++++++ 12 files changed, 139 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 982100b..8d4c8f5 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Or manually update `require` block of `composer.json` and run `composer update`. ```json { "require": { - "dragon-code/laravel-cache": "^3.9" + "dragon-code/laravel-cache": "^3.11" } } ``` @@ -201,6 +201,9 @@ $cache->doesntHave(); $cache->forget(); // Will remove the key from the cache. + +$cache->flush(); +// Clears keys or tags by value ``` ```php @@ -237,6 +240,9 @@ $cache->doesntHave(); $cache->forget(); // Will remove the key from the cache. + +$cache->flush(); +// Clears keys or tags by value ``` #### Method Call Chain @@ -428,6 +434,9 @@ $cache->doesntHave(); $cache->forget(); // Will remove the key from the cache. + +$cache->flush(); +// Clears keys or tags by value ``` To retrieve a tagged cache item, pass the same ordered list of tags to the tags method and then call the get method with @@ -451,6 +460,9 @@ $cache->tags('actor')->get(); $cache->tags('author')->get(); // Returns `null` + +$cache->tags('author')->flush(); +// Clears keys or tags by value ``` > See the official Laravel [documentation](https://laravel.com/docs/cache#accessing-tagged-cache-items). diff --git a/src/Services/Cache.php b/src/Services/Cache.php index 4489070..f9dfb40 100644 --- a/src/Services/Cache.php +++ b/src/Services/Cache.php @@ -109,6 +109,13 @@ public function forget(): static return $this; } + public function flush(): static + { + $this->manager()->flush(); + + return $this; + } + public function has(): bool { return $this->manager()->has($this->getKey()); diff --git a/src/Services/Storages/Disabled.php b/src/Services/Storages/Disabled.php index 7f0dea6..1528aba 100644 --- a/src/Services/Storages/Disabled.php +++ b/src/Services/Storages/Disabled.php @@ -32,4 +32,6 @@ public function has(string $key): bool { return false; } + + public function flush(): void {} } diff --git a/src/Services/Storages/MainStore.php b/src/Services/Storages/MainStore.php index cabbd55..397c2ac 100644 --- a/src/Services/Storages/MainStore.php +++ b/src/Services/Storages/MainStore.php @@ -49,4 +49,9 @@ public function has(string $key): bool { return Cache::has($key); } + + public function flush(): void + { + Cache::flush(); + } } diff --git a/src/Services/Storages/TaggedStore.php b/src/Services/Storages/TaggedStore.php index b42d354..7dc47ed 100644 --- a/src/Services/Storages/TaggedStore.php +++ b/src/Services/Storages/TaggedStore.php @@ -60,6 +60,11 @@ public function has(string $key): bool return $this->cache()->has($key); } + public function flush(): void + { + $this->cache()->flush(); + } + protected function cache(): TaggedCache { return Cache::tags($this->tags); diff --git a/src/Support/CacheManager.php b/src/Support/CacheManager.php index f923399..20545fb 100644 --- a/src/Support/CacheManager.php +++ b/src/Support/CacheManager.php @@ -66,6 +66,11 @@ public function doesntHave(string $key): bool return ! $this->has($key); } + public function flush(): void + { + $this->instance()->flush(); + } + protected function instance(): Store { return match (true) { diff --git a/tests/Cache/NotWhen/Simple/ArrayTest.php b/tests/Cache/NotWhen/Simple/ArrayTest.php index 3042d29..f67dfcb 100644 --- a/tests/Cache/NotWhen/Simple/ArrayTest.php +++ b/tests/Cache/NotWhen/Simple/ArrayTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\NotWhen\Simple; +use DragonCode\Cache\Services\Cache; use Tests\Cache\NotWhen\Base; class ArrayTest extends Base @@ -51,6 +52,22 @@ public function testForget() $this->assertNull($this->cache()->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has()); diff --git a/tests/Cache/NotWhen/Simple/FileTest.php b/tests/Cache/NotWhen/Simple/FileTest.php index 547af5e..5bc974e 100644 --- a/tests/Cache/NotWhen/Simple/FileTest.php +++ b/tests/Cache/NotWhen/Simple/FileTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\NotWhen\Simple; +use DragonCode\Cache\Services\Cache; use Tests\Cache\NotWhen\Base; class FileTest extends Base @@ -51,6 +52,22 @@ public function testForget() $this->assertNull($this->cache()->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has()); diff --git a/tests/Cache/NotWhen/Simple/RedisTest.php b/tests/Cache/NotWhen/Simple/RedisTest.php index 03924f0..993e4d7 100644 --- a/tests/Cache/NotWhen/Simple/RedisTest.php +++ b/tests/Cache/NotWhen/Simple/RedisTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\NotWhen\Simple; +use DragonCode\Cache\Services\Cache; use Illuminate\Support\Facades\Auth; use Tests\Cache\NotWhen\Base; use Tests\Fixtures\Models\User; @@ -73,6 +74,22 @@ public function testForget() $this->assertNull($this->cache(['cache'])->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has()); diff --git a/tests/Cache/When/Simple/ArrayTest.php b/tests/Cache/When/Simple/ArrayTest.php index 8ccf36f..fc3e3e2 100644 --- a/tests/Cache/When/Simple/ArrayTest.php +++ b/tests/Cache/When/Simple/ArrayTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\When\Simple; +use DragonCode\Cache\Services\Cache; use Tests\Cache\When\Base; class ArrayTest extends Base @@ -61,6 +62,22 @@ public function testForget() $this->assertNull($this->cache()->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has()); diff --git a/tests/Cache/When/Simple/FileTest.php b/tests/Cache/When/Simple/FileTest.php index f8a70d6..dc696cc 100644 --- a/tests/Cache/When/Simple/FileTest.php +++ b/tests/Cache/When/Simple/FileTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\When\Simple; +use DragonCode\Cache\Services\Cache; use Tests\Cache\When\Base; class FileTest extends Base @@ -63,6 +64,22 @@ public function testForget() $this->assertNull($this->cache()->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has()); diff --git a/tests/Cache/When/Simple/RedisTest.php b/tests/Cache/When/Simple/RedisTest.php index 215b201..f945ae6 100644 --- a/tests/Cache/When/Simple/RedisTest.php +++ b/tests/Cache/When/Simple/RedisTest.php @@ -4,6 +4,7 @@ namespace Tests\Cache\When\Simple; +use DragonCode\Cache\Services\Cache; use Illuminate\Support\Facades\Auth; use Tests\Cache\When\Base; use Tests\Fixtures\Models\User; @@ -72,6 +73,22 @@ public function testForget() $this->assertNull($this->cache(['cache'])->get()); } + public function testFlush() + { + $tags = Cache::make()->tags('foo', 'bar'); + $cache = (clone $tags)->key('qwerty'); + + $this->assertNull($cache->get()); + + $cache->put('asd'); + + $this->assertSame('asd', $cache->get()); + + $tags->flush(); + + $this->assertNull($cache->get()); + } + public function testHas() { $this->assertFalse($this->cache()->has());