Skip to content

Commit f279df0

Browse files
committed
Added base tags to ensure full resource cache purging
1 parent 1895fc6 commit f279df0

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ $alice = Kid::whereName('Alice')->cacheFor(60)->cacheTags(['kids'])->first();
9090
$bob = Kid::whereName('Bob')->cacheFor(60)->cacheTags(['kids'])->first();
9191
```
9292

93+
### Global Cache Invalidation
94+
95+
To invalidate all the cache for a specific model, use the `flushAllQueryCache` method.
96+
97+
The package automatically appends a list of tags, called **base tags** on each query coming from a model. It defaults to the full model class name.
98+
99+
You can still use your custom tags on the queries and they will work like usual:
100+
101+
```php
102+
Kid::flushAllQueryCache();
103+
```
104+
105+
In case you want to change the base tags, you can do so in your model.
106+
107+
```php
108+
class Kid extends Model
109+
{
110+
use QueryCacheable;
111+
112+
/**
113+
* Set the base cache tags that will be present
114+
* on all queries.
115+
*
116+
* @return array
117+
*/
118+
protected function getCacheBaseTags(): array
119+
{
120+
return [
121+
'custom_tag',
122+
];
123+
}
124+
}
125+
126+
// Automatically works with `custom_tag`
127+
Kid::flushAllQueryCache();
128+
```
129+
93130
## Relationship Caching
94131
Relationships are just another queries. They can be intercepted and modified before the database is hit with the query. The following example needs the `Order` model (or the model associated with the `orders` relationship) to include the `QueryCacheable` trait.
95132

src/Traits/QueryCacheModule.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ trait QueryCacheModule
2222
*/
2323
protected $cacheTags = null;
2424

25+
/**
26+
* The tags for the query cache that
27+
* will be present on all queries.
28+
*
29+
* @var null|array
30+
*/
31+
protected $cacheBaseTags = null;
32+
2533
/**
2634
* The cache driver to be used.
2735
*
@@ -170,6 +178,18 @@ public function flushQueryCache(array $tags = []): bool
170178
return true;
171179
}
172180

181+
/**
182+
* Flush all cached resources with the default model tag.
183+
*
184+
* @return bool
185+
*/
186+
public function flushAllQueryCache(): bool
187+
{
188+
return self::flushQueryCache(
189+
$this->getCacheBaseTags() ?: []
190+
);
191+
}
192+
173193
/**
174194
* Flush the cache for a specific tag.
175195
*
@@ -272,6 +292,20 @@ public function cacheDriver(string $cacheDriver)
272292
return $this;
273293
}
274294

295+
/**
296+
* Set the base cache tags; the tags
297+
* that will be present on all cached queries.
298+
*
299+
* @param array $tags
300+
* @return \Rennokki\QueryCache\Query\Builder
301+
*/
302+
public function cacheBaseTags(array $tags = [])
303+
{
304+
$this->cacheBaseTags = $tags;
305+
306+
return $this;
307+
}
308+
275309
/**
276310
* Use a plain key instead of a hashed one in the cache driver.
277311
*
@@ -302,9 +336,13 @@ public function getCacheDriver()
302336
public function getCache()
303337
{
304338
$cache = $this->getCacheDriver();
305-
$tags = $this->getCacheTags();
306339

307-
return $tags ? $cache->tags($tags) : $cache;
340+
$tags = array_merge(
341+
$this->getCacheTags() ?: [],
342+
$this->getCacheBaseTags() ?: []
343+
);
344+
345+
return $cache->tags($tags);
308346
}
309347

310348
/**
@@ -348,6 +386,16 @@ public function getCacheTags()
348386
return $this->cacheTags;
349387
}
350388

389+
/**
390+
* Get the base cache tags attribute.
391+
*
392+
* @return array|null
393+
*/
394+
public function getCacheBaseTags()
395+
{
396+
return $this->cacheBaseTags;
397+
}
398+
351399
/**
352400
* Get the cache prefix attribute.
353401
*

src/Traits/QueryCacheable.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ protected function newBaseQueryBuilder()
4141
$builder->withPlainKey();
4242
}
4343

44-
return $builder;
44+
return $builder
45+
->cacheBaseTags($this->getCacheBaseTags());
46+
}
47+
48+
/**
49+
* Set the base cache tags that will be present
50+
* on all queries.
51+
*
52+
* @return array
53+
*/
54+
protected function getCacheBaseTags(): array
55+
{
56+
return [
57+
(string) Self::class,
58+
];
4559
}
4660
}

tests/MethodsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ public function test_cache_flush_with_more_tags()
8989
$this->assertNull($cache);
9090
}
9191

92+
public function test_cache_flush_with_default_tags_attached()
93+
{
94+
$post = factory(Post::class)->create();
95+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
96+
97+
$cache = Cache::tags(['test', Post::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
98+
$this->assertNotNull($cache);
99+
100+
Post::flushAllQueryCache();
101+
102+
$cache = Cache::tags(['test', Post::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
103+
104+
$this->assertNull($cache);
105+
}
106+
92107
public function test_hashed_key()
93108
{
94109
$kid = factory(Kid::class)->create();

0 commit comments

Comments
 (0)