Skip to content

Commit 4646632

Browse files
Added the ability to work with tags without keys
1 parent 3eea010 commit 4646632

File tree

12 files changed

+139
-1
lines changed

12 files changed

+139
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
2121
```json
2222
{
2323
"require": {
24-
"dragon-code/laravel-cache": "^3.9"
24+
"dragon-code/laravel-cache": "^3.11"
2525
}
2626
}
2727
```
@@ -201,6 +201,9 @@ $cache->doesntHave();
201201

202202
$cache->forget();
203203
// Will remove the key from the cache.
204+
205+
$cache->flush();
206+
// Clears keys or tags by value
204207
```
205208

206209
```php
@@ -237,6 +240,9 @@ $cache->doesntHave();
237240

238241
$cache->forget();
239242
// Will remove the key from the cache.
243+
244+
$cache->flush();
245+
// Clears keys or tags by value
240246
```
241247

242248
#### Method Call Chain
@@ -428,6 +434,9 @@ $cache->doesntHave();
428434

429435
$cache->forget();
430436
// Will remove the key from the cache.
437+
438+
$cache->flush();
439+
// Clears keys or tags by value
431440
```
432441

433442
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();
451460

452461
$cache->tags('author')->get();
453462
// Returns `null`
463+
464+
$cache->tags('author')->flush();
465+
// Clears keys or tags by value
454466
```
455467

456468
> See the official Laravel [documentation](https://laravel.com/docs/cache#accessing-tagged-cache-items).

src/Services/Cache.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ public function forget(): static
109109
return $this;
110110
}
111111

112+
public function flush(): static
113+
{
114+
$this->manager()->flush();
115+
116+
return $this;
117+
}
118+
112119
public function has(): bool
113120
{
114121
return $this->manager()->has($this->getKey());

src/Services/Storages/Disabled.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public function has(string $key): bool
3232
{
3333
return false;
3434
}
35+
36+
public function flush(): void {}
3537
}

src/Services/Storages/MainStore.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ public function has(string $key): bool
4949
{
5050
return Cache::has($key);
5151
}
52+
53+
public function flush(): void
54+
{
55+
Cache::flush();
56+
}
5257
}

src/Services/Storages/TaggedStore.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function has(string $key): bool
6060
return $this->cache()->has($key);
6161
}
6262

63+
public function flush(): void
64+
{
65+
$this->cache()->flush();
66+
}
67+
6368
protected function cache(): TaggedCache
6469
{
6570
return Cache::tags($this->tags);

src/Support/CacheManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public function doesntHave(string $key): bool
6666
return ! $this->has($key);
6767
}
6868

69+
public function flush(): void
70+
{
71+
$this->instance()->flush();
72+
}
73+
6974
protected function instance(): Store
7075
{
7176
return match (true) {

tests/Cache/NotWhen/Simple/ArrayTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Cache\NotWhen\Simple;
66

7+
use DragonCode\Cache\Services\Cache;
78
use Tests\Cache\NotWhen\Base;
89

910
class ArrayTest extends Base
@@ -51,6 +52,22 @@ public function testForget()
5152
$this->assertNull($this->cache()->get());
5253
}
5354

55+
public function testFlush()
56+
{
57+
$tags = Cache::make()->tags('foo', 'bar');
58+
$cache = (clone $tags)->key('qwerty');
59+
60+
$this->assertNull($cache->get());
61+
62+
$cache->put('asd');
63+
64+
$this->assertSame('asd', $cache->get());
65+
66+
$tags->flush();
67+
68+
$this->assertNull($cache->get());
69+
}
70+
5471
public function testHas()
5572
{
5673
$this->assertFalse($this->cache()->has());

tests/Cache/NotWhen/Simple/FileTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Cache\NotWhen\Simple;
66

7+
use DragonCode\Cache\Services\Cache;
78
use Tests\Cache\NotWhen\Base;
89

910
class FileTest extends Base
@@ -51,6 +52,22 @@ public function testForget()
5152
$this->assertNull($this->cache()->get());
5253
}
5354

55+
public function testFlush()
56+
{
57+
$tags = Cache::make()->tags('foo', 'bar');
58+
$cache = (clone $tags)->key('qwerty');
59+
60+
$this->assertNull($cache->get());
61+
62+
$cache->put('asd');
63+
64+
$this->assertSame('asd', $cache->get());
65+
66+
$tags->flush();
67+
68+
$this->assertNull($cache->get());
69+
}
70+
5471
public function testHas()
5572
{
5673
$this->assertFalse($this->cache()->has());

tests/Cache/NotWhen/Simple/RedisTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Cache\NotWhen\Simple;
66

7+
use DragonCode\Cache\Services\Cache;
78
use Illuminate\Support\Facades\Auth;
89
use Tests\Cache\NotWhen\Base;
910
use Tests\Fixtures\Models\User;
@@ -73,6 +74,22 @@ public function testForget()
7374
$this->assertNull($this->cache(['cache'])->get());
7475
}
7576

77+
public function testFlush()
78+
{
79+
$tags = Cache::make()->tags('foo', 'bar');
80+
$cache = (clone $tags)->key('qwerty');
81+
82+
$this->assertNull($cache->get());
83+
84+
$cache->put('asd');
85+
86+
$this->assertSame('asd', $cache->get());
87+
88+
$tags->flush();
89+
90+
$this->assertNull($cache->get());
91+
}
92+
7693
public function testHas()
7794
{
7895
$this->assertFalse($this->cache()->has());

tests/Cache/When/Simple/ArrayTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Cache\When\Simple;
66

7+
use DragonCode\Cache\Services\Cache;
78
use Tests\Cache\When\Base;
89

910
class ArrayTest extends Base
@@ -61,6 +62,22 @@ public function testForget()
6162
$this->assertNull($this->cache()->get());
6263
}
6364

65+
public function testFlush()
66+
{
67+
$tags = Cache::make()->tags('foo', 'bar');
68+
$cache = (clone $tags)->key('qwerty');
69+
70+
$this->assertNull($cache->get());
71+
72+
$cache->put('asd');
73+
74+
$this->assertSame('asd', $cache->get());
75+
76+
$tags->flush();
77+
78+
$this->assertNull($cache->get());
79+
}
80+
6481
public function testHas()
6582
{
6683
$this->assertFalse($this->cache()->has());

0 commit comments

Comments
 (0)