Skip to content

Commit 0b0a5bb

Browse files
committed
Finish implementing optional cache disabling
1 parent 4428a3e commit 0b0a5bb

6 files changed

+464
-17
lines changed

src/CachedBuilder.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
44
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
55

6+
/**
7+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
8+
*/
69
class CachedBuilder extends EloquentBuilder
710
{
811
use Cachable;
912

10-
protected $isCacheDisabled = false;
13+
protected $isCachable = true;
1114

12-
public function __call(string $method, array $parameters)
15+
public function avg($column)
1316
{
14-
if (method_exists($this, $method)) {
15-
if (isCacheDisabled) {
16-
return parent::$method($parameters);
17-
}
18-
19-
$this->$method($parameters);
17+
if (! $this->isCachable) {
18+
return parent::avg($column);
2019
}
21-
}
2220

23-
public function avg($column)
24-
{
2521
return $this->cache($this->makeCacheTags())
2622
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
2723
return parent::avg($column);
@@ -30,6 +26,10 @@ public function avg($column)
3026

3127
public function count($columns = ['*'])
3228
{
29+
if (! $this->isCachable) {
30+
return parent::count($columns);
31+
}
32+
3333
return $this->cache($this->makeCacheTags())
3434
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
3535
return parent::count($columns);
@@ -38,6 +38,10 @@ public function count($columns = ['*'])
3838

3939
public function cursor()
4040
{
41+
if (! $this->isCachable) {
42+
return collect(parent::cursor());
43+
}
44+
4145
return $this->cache($this->makeCacheTags())
4246
->rememberForever($this->makeCacheKey() . "-cursor", function () {
4347
return collect(parent::cursor());
@@ -52,11 +56,22 @@ public function delete()
5256
return parent::delete();
5357
}
5458

59+
public function disableCache()
60+
{
61+
$this->isCachable = false;
62+
63+
return $this;
64+
}
65+
5566
/**
5667
* @SuppressWarnings(PHPMD.ShortVariable)
5768
*/
5869
public function find($id, $columns = ['*'])
5970
{
71+
if (! $this->isCachable) {
72+
return parent::find($id, $columns);
73+
}
74+
6075
return $this->cache($this->makeCacheTags())
6176
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
6277
return parent::find($id, $columns);
@@ -65,6 +80,10 @@ public function find($id, $columns = ['*'])
6580

6681
public function first($columns = ['*'])
6782
{
83+
if (! $this->isCachable) {
84+
return parent::first($columns);
85+
}
86+
6887
return $this->cache($this->makeCacheTags())
6988
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
7089
return parent::first($columns);
@@ -73,6 +92,10 @@ public function first($columns = ['*'])
7392

7493
public function get($columns = ['*'])
7594
{
95+
if (! $this->isCachable) {
96+
return parent::get($columns);
97+
}
98+
7699
return $this->cache($this->makeCacheTags())
77100
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
78101
return parent::get($columns);
@@ -81,6 +104,10 @@ public function get($columns = ['*'])
81104

82105
public function max($column)
83106
{
107+
if (! $this->isCachable) {
108+
return parent::max($column);
109+
}
110+
84111
return $this->cache($this->makeCacheTags())
85112
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
86113
return parent::max($column);
@@ -89,6 +116,10 @@ public function max($column)
89116

90117
public function min($column)
91118
{
119+
if (! $this->isCachable) {
120+
return parent::min($column);
121+
}
122+
92123
return $this->cache($this->makeCacheTags())
93124
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
94125
return parent::min($column);
@@ -97,6 +128,10 @@ public function min($column)
97128

98129
public function pluck($column, $key = null)
99130
{
131+
if (! $this->isCachable) {
132+
return parent::pluck($column, $key);
133+
}
134+
100135
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
101136

102137
if ($key) {
@@ -111,6 +146,10 @@ public function pluck($column, $key = null)
111146

112147
public function sum($column)
113148
{
149+
if (! $this->isCachable) {
150+
return parent::sum($column);
151+
}
152+
114153
return $this->cache($this->makeCacheTags())
115154
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
116155
return parent::sum($column);

src/CachedModel.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ abstract class CachedModel extends Model
1414
{
1515
public function newEloquentBuilder($query)
1616
{
17+
if (session('genealabs-laravel-model-caching-is-disabled')) {
18+
session()->forget('genealabs-laravel-model-caching-is-disabled');
19+
20+
return new EloquentBuilder($query);
21+
}
22+
1723
return new Builder($query);
1824
}
1925

@@ -52,11 +58,11 @@ public function cache(array $tags = [])
5258
return $cache;
5359
}
5460

55-
public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
61+
public function disableCache() : self
5662
{
57-
$query->disableCache();
63+
session(['genealabs-laravel-model-caching-is-disabled' => true]);
5864

59-
return $query;
65+
return $this;
6066
}
6167

6268
public function flushCache(array $tags = [])

tests/Unit/CachedBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929
cache()->flush();
3030
$publishers = factory(Publisher::class, 10)->create();
3131
factory(Author::class, 10)->create()
32-
->each(function($author) use ($publishers) {
32+
->each(function ($author) use ($publishers) {
3333
factory(Book::class, random_int(2, 10))->make()
3434
->each(function ($book) use ($author, $publishers) {
3535
$book->author()->associate($author);

tests/Unit/CachedModelTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ public function testAllModelResultsCreatesCache()
5252
'genealabslaravelmodelcachingtestsfixturesauthor',
5353
];
5454

55-
$cachedResults = cache()->tags($tags)
55+
$cachedResults = cache()
56+
->tags($tags)
5657
->get($key);
57-
$liveResults = (new UncachedAuthor)->all();
58+
$liveResults = (new UncachedAuthor)
59+
->all();
5860

5961
$this->assertEquals($authors, $cachedResults);
6062
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
6163
}
6264

65+
/**
66+
* @group test
67+
**/
6368
public function testScopeDisablesCaching()
6469
{
6570
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';

0 commit comments

Comments
 (0)