Skip to content

Commit 4428a3e

Browse files
committed
WIP
1 parent 62998a2 commit 4428a3e

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
convertNoticesToExceptions="true"
77
convertWarningsToExceptions="true"
88
processIsolation="false"
9-
stopOnFailure="true"
9+
stopOnFailure="false"
1010
syntaxCheck="false"
1111
bootstrap="vendor/autoload.php"
1212
>

src/CachedBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ class CachedBuilder extends EloquentBuilder
77
{
88
use Cachable;
99

10+
protected $isCacheDisabled = false;
11+
12+
public function __call(string $method, array $parameters)
13+
{
14+
if (method_exists($this, $method)) {
15+
if (isCacheDisabled) {
16+
return parent::$method($parameters);
17+
}
18+
19+
$this->$method($parameters);
20+
}
21+
}
22+
1023
public function avg($column)
1124
{
1225
return $this->cache($this->makeCacheTags())

src/CachedModel.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
44
use Illuminate\Cache\CacheManager;
55
use Illuminate\Cache\TaggableStore;
6+
use Illuminate\Cache\TaggedCache;
7+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
68
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Database\Eloquent\Relations\Relation;
8-
use LogicException;
9-
10-
use Illuminate\Cache\TaggedCache;
1110
use Illuminate\Support\Collection;
11+
use LogicException;
1212

1313
abstract class CachedModel extends Model
1414
{
@@ -52,6 +52,13 @@ public function cache(array $tags = [])
5252
return $cache;
5353
}
5454

55+
public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
56+
{
57+
$query->disableCache();
58+
59+
return $query;
60+
}
61+
5562
public function flushCache(array $tags = [])
5663
{
5764
$this->cache($tags)->flush();

tests/Unit/CachedModelTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp()
2424
cache()->flush();
2525
$publishers = factory(Publisher::class, 10)->create();
2626
factory(Author::class, 10)->create()
27-
->each(function($author) use ($publishers) {
27+
->each(function ($author) use ($publishers) {
2828
factory(Book::class, random_int(2, 10))->make()
2929
->each(function ($book) use ($author, $publishers) {
3030
$book->author()->associate($author);
@@ -59,4 +59,21 @@ public function testAllModelResultsCreatesCache()
5959
$this->assertEquals($authors, $cachedResults);
6060
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
6161
}
62+
63+
public function testScopeDisablesCaching()
64+
{
65+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
66+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
67+
$authors = (new Author)
68+
->where("name", "Bruno")
69+
->disableCache()
70+
->get();
71+
72+
$cachedResults = cache()
73+
->tags($tags)
74+
->get($key);
75+
76+
$this->assertNull($cachedResults);
77+
$this->assertNotEquals($authors, $cachedResults);
78+
}
6279
}

0 commit comments

Comments
 (0)