Skip to content

Commit b116416

Browse files
committed
Merge branch 'master' into develop
2 parents 3f1fa22 + 3e6be63 commit b116416

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ cache for that single query. Disabling cache via the config flag still works.
130130

131131
**Recommendation: use option #1 in all your seeder queries to avoid pulling in
132132
cached information when reseeding multiple times.**
133-
You can disable a given query by using `disableCache()` in the query chain. It must be placed immediately behind the model, and not in the query chain. For example:
133+
You can disable a given query by using `disableCache()` anywhere in the query chain. For example:
134134
```php
135135
$results = $myModel->disableCache()->where('field', $value)->get();
136136
```

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)