Skip to content

Commit 35c88b4

Browse files
committed
Implement disabling of cache as local scope
1 parent fe19bd3 commit 35c88b4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Traits/Caching.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function cache(array $tags = [])
2525
return $cache;
2626
}
2727

28-
public function disableCache()
28+
public function disableModelCaching()
2929
{
3030
$this->isCachable = false;
3131

src/Traits/ModelCaching.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function newEloquentBuilder($query)
5252
return new CachedBuilder($query);
5353
}
5454

55+
public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
56+
{
57+
$query = $query->disableModelCaching();
58+
59+
return $query;
60+
}
61+
5562
public function scopeWithCacheCooldownSeconds(
5663
EloquentBuilder $query,
5764
int $seconds

tests/Integration/DisabledCachedModelTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,43 @@ class DisabledCachedModelTest extends IntegrationTestCase
1717
{
1818
use RefreshDatabase;
1919

20-
public function testAllModelResultsIsNotCached()
20+
public function testCacheCanBeDisabledOnModel()
2121
{
2222
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
2323
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
2424
$authors = (new Author)
2525
->disableCache()
26-
->all();
26+
->get();
2727

2828
$cachedResults = $this->cache()
2929
->tags($tags)
3030
->get($key);
3131
$liveResults = (new UncachedAuthor)
32-
->all();
32+
->get();
3333

3434
$this->assertEmpty($liveResults->diffAssoc($authors));
3535
$this->assertNull($cachedResults);
3636
}
37+
38+
public function testCacheCanBeDisabledOnQuery()
39+
{
40+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
41+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
42+
$authors = (new Author)
43+
->with('books')
44+
->disableCache()
45+
->get()
46+
->keyBy("id");
47+
48+
$cachedResults = $this->cache()
49+
->tags($tags)
50+
->get($key);
51+
$liveResults = (new UncachedAuthor)
52+
->with('books')
53+
->get()
54+
->keyBy("id");
55+
56+
$this->assertNull($cachedResults);
57+
$this->assertEmpty($liveResults->diffKeys($authors));
58+
}
3759
}

0 commit comments

Comments
 (0)