Skip to content

Commit 98331a7

Browse files
authored
Merge pull request #6 from rennokki/feature/flush-tags-does-not-work-properly
[feature] Flush each tag on flush cache
2 parents 8606561 + a47a73c commit 98331a7

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ $shelfTwoBooks = Book::whereShelf(2)->cacheFor(60)->cacheTags(['shelf:2'])->get(
7777

7878
// After flushing the cache for shelf:1, the query of$shelfTwoBooks will still hit the cache if re-called again.
7979
Book::flushQueryCache(['shelf:1']);
80+
81+
// Flushing also works for both tags, invalidating them both, not just the one tagged with shelf:1
82+
Book::flushQueryCache(['shelf:1', 'shelf:2']);
8083
```
8184

8285
Be careful tho - specifying cache tags does not change the behaviour of key storage.

src/Query/Builder.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Builder extends BaseBuilder
1919
* The tags for the query cache. Can be useful
2020
* if flushing cache for specific tags only.
2121
*
22-
* @var array
22+
* @var null|array
2323
*/
24-
protected $cacheTags = [];
24+
protected $cacheTags = null;
2525

2626
/**
2727
* The cache driver to be used.
@@ -159,21 +159,41 @@ public function generatePlainCacheKey(string $method = 'get', $id = null, $appen
159159
}
160160

161161
/**
162-
* Flush the cache for the current model or
163-
* flush cache containing specific tags.
162+
* Flush the cache that contains specific tags.
164163
*
165164
* @param array $tags
166165
* @return bool
167166
*/
168-
public function flushQueryCache(array $tags = ['leqc']): bool
167+
public function flushQueryCache(array $tags = []): bool
169168
{
170169
$cache = $this->getCacheDriver();
171170

172171
if (! method_exists($cache, 'tags')) {
173172
return false;
174173
}
175174

176-
return $cache->tags($tags ?: $this->cacheTags)->flush();
175+
foreach ($tags as $tag) {
176+
self::flushQueryCacheWithTag($tag);
177+
}
178+
179+
return true;
180+
}
181+
182+
/**
183+
* Flush the cache for a specific tag.
184+
*
185+
* @param string $tag
186+
* @return bool
187+
*/
188+
public function flushQueryCacheWithTag(string $tag): bool
189+
{
190+
$cache = $this->getCacheDriver();
191+
192+
if (! method_exists($cache, 'tags')) {
193+
return false;
194+
}
195+
196+
return $cache->tags($tag)->flush();
177197
}
178198

179199
/**

tests/MethodsTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,30 @@ public function test_cache_flush_without_the_right_tag()
6565
$this->assertNotNull($cache);
6666

6767
Post::flushQueryCache(['test2']);
68+
Post::flushQueryCacheWithTag('test2');
6869

6970
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
7071
$this->assertNotNull($cache);
7172
}
7273

74+
public function test_cache_flush_with_more_tags()
75+
{
76+
$post = factory(Post::class)->create();
77+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
78+
79+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
80+
$this->assertNotNull($cache);
81+
82+
Post::flushQueryCache([
83+
'test',
84+
'test2',
85+
'test3',
86+
]);
87+
88+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
89+
$this->assertNull($cache);
90+
}
91+
7392
public function test_hashed_key()
7493
{
7594
$kid = factory(Kid::class)->create();

0 commit comments

Comments
 (0)