File tree Expand file tree Collapse file tree 4 files changed +42
-5
lines changed Expand file tree Collapse file tree 4 files changed +42
-5
lines changed Original file line number Diff line number Diff line change 66 convertNoticesToExceptions =" true"
77 convertWarningsToExceptions =" true"
88 processIsolation =" false"
9- stopOnFailure =" true "
9+ stopOnFailure =" false "
1010 syntaxCheck =" false"
1111 bootstrap =" vendor/autoload.php"
1212>
Original file line number Diff line number Diff 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 ())
Original file line number Diff line number Diff line change 33use GeneaLabs \LaravelModelCaching \CachedBuilder as Builder ;
44use Illuminate \Cache \CacheManager ;
55use Illuminate \Cache \TaggableStore ;
6+ use Illuminate \Cache \TaggedCache ;
7+ use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
68use Illuminate \Database \Eloquent \Model ;
79use Illuminate \Database \Eloquent \Relations \Relation ;
8- use LogicException ;
9-
10- use Illuminate \Cache \TaggedCache ;
1110use Illuminate \Support \Collection ;
11+ use LogicException ;
1212
1313abstract 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 ();
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments