Skip to content

Commit 15c4e5b

Browse files
committed
Merge branch 'hotfix/fix-all-query-cache-disabling'
2 parents 250d26f + 70653a6 commit 15c4e5b

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

.scrutinizer.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
filter:
22
excluded_paths:
33
- "tests/"
4+
build:
5+
environment:
6+
variables:
7+
REDIS_HOST: '127.0.0.1'
8+
nodes:
9+
tests: false

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ There are two methods by which model-caching can be disabled:
110110
1. Use `->disableCache()` in a query-by-query instance.
111111
2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file.
112112

113+
**EXCEPTION:** currently the `::all()` method cannot be disabled by doing something
114+
like this: `$model->disableCache()->all()`, because it is a static method. To
115+
work around this, use the `->get()` method if you really need to disable the
116+
cache for that single query. Disabling cache via the config flag still works.
117+
113118
**Recommendation: use option #1 in all your seeder queries to avoid pulling in
114119
cached information when reseeding multiple times.**
115120
You can disable a given query by using `disableCache()` in the query chain, and

src/Traits/Cachable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public static function bootCachable()
8686

8787
public static function all($columns = ['*'])
8888
{
89+
if (config('laravel-model-caching.disabled')) {
90+
return parent::all($columns);
91+
}
92+
8993
$class = get_called_class();
9094
$instance = new $class;
9195
$tags = [str_slug(get_called_class())];

tests/Unit/CachedModelTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function testAllModelResultsCreatesCache()
2525
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
2626
];
2727

28-
$cachedResults = $this->cache()
28+
$cachedResults = $this
29+
->cache()
2930
->tags($tags)
3031
->get($key)['value'];
3132
$liveResults = (new UncachedAuthor)
@@ -51,4 +52,25 @@ public function testScopeDisablesCaching()
5152
$this->assertNull($cachedResults);
5253
$this->assertNotEquals($authors, $cachedResults);
5354
}
55+
56+
public function testAllMethodCachingCanBeDisabledViaConfig()
57+
{
58+
config(['laravel-model-caching.disabled' => true]);
59+
$authors = (new Author)
60+
->all();
61+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
62+
$tags = [
63+
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
64+
];
65+
config(['laravel-model-caching.disabled' => false]);
66+
67+
$cachedResults = $this
68+
->cache()
69+
->tags($tags)
70+
->get($key)['value'];
71+
72+
$this->assertEmpty($cachedResults);
73+
$this->assertNotEmpty($authors);
74+
$this->assertCount(10, $authors);
75+
}
5476
}

tests/Unit/Traits/CachableTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,24 @@ public function testSetCachePrefixAttribute()
6060
$this->assertNotNull($results);
6161
}
6262

63-
// public function testAllReturnsCollection()
64-
// {
65-
// (new Author)->truncate();
66-
// factory(Author::class, 1)->create();
67-
// $authors = (new Author)->all();
68-
//
69-
// $cachedResults = $this
70-
// ->cache()
71-
// ->tags([
72-
// 'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
73-
// ])
74-
// ->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
75-
// $liveResults = (new UncachedAuthor)->all();
76-
//
77-
// $this->assertInstanceOf(Collection::class, $authors);
78-
// $this->assertInstanceOf(Collection::class, $cachedResults);
79-
// $this->assertInstanceOf(Collection::class, $liveResults);
80-
// }
63+
public function testAllReturnsCollection()
64+
{
65+
(new Author)->truncate();
66+
factory(Author::class, 1)->create();
67+
$authors = (new Author)->all();
68+
69+
$cachedResults = $this
70+
->cache()
71+
->tags([
72+
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
73+
])
74+
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
75+
$liveResults = (new UncachedAuthor)->all();
76+
77+
$this->assertInstanceOf(Collection::class, $authors);
78+
$this->assertInstanceOf(Collection::class, $cachedResults);
79+
$this->assertInstanceOf(Collection::class, $liveResults);
80+
}
8181

8282
public function testsCacheFlagDisablesCaching()
8383
{

0 commit comments

Comments
 (0)