File tree 4 files changed +42
-5
lines changed
4 files changed +42
-5
lines changed Original file line number Diff line number Diff line change 6
6
convertNoticesToExceptions =" true"
7
7
convertWarningsToExceptions =" true"
8
8
processIsolation =" false"
9
- stopOnFailure =" true "
9
+ stopOnFailure =" false "
10
10
syntaxCheck =" false"
11
11
bootstrap =" vendor/autoload.php"
12
12
>
Original file line number Diff line number Diff line change @@ -7,6 +7,19 @@ class CachedBuilder extends EloquentBuilder
7
7
{
8
8
use Cachable;
9
9
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
+
10
23
public function avg ($ column )
11
24
{
12
25
return $ this ->cache ($ this ->makeCacheTags ())
Original file line number Diff line number Diff line change 3
3
use GeneaLabs \LaravelModelCaching \CachedBuilder as Builder ;
4
4
use Illuminate \Cache \CacheManager ;
5
5
use Illuminate \Cache \TaggableStore ;
6
+ use Illuminate \Cache \TaggedCache ;
7
+ use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
6
8
use Illuminate \Database \Eloquent \Model ;
7
9
use Illuminate \Database \Eloquent \Relations \Relation ;
8
- use LogicException ;
9
-
10
- use Illuminate \Cache \TaggedCache ;
11
10
use Illuminate \Support \Collection ;
11
+ use LogicException ;
12
12
13
13
abstract class CachedModel extends Model
14
14
{
@@ -52,6 +52,13 @@ public function cache(array $tags = [])
52
52
return $ cache ;
53
53
}
54
54
55
+ public function scopeDisableCache (EloquentBuilder $ query ) : EloquentBuilder
56
+ {
57
+ $ query ->disableCache ();
58
+
59
+ return $ query ;
60
+ }
61
+
55
62
public function flushCache (array $ tags = [])
56
63
{
57
64
$ this ->cache ($ tags )->flush ();
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ public function setUp()
24
24
cache ()->flush ();
25
25
$ publishers = factory (Publisher::class, 10 )->create ();
26
26
factory (Author::class, 10 )->create ()
27
- ->each (function ($ author ) use ($ publishers ) {
27
+ ->each (function ($ author ) use ($ publishers ) {
28
28
factory (Book::class, random_int (2 , 10 ))->make ()
29
29
->each (function ($ book ) use ($ author , $ publishers ) {
30
30
$ book ->author ()->associate ($ author );
@@ -59,4 +59,21 @@ public function testAllModelResultsCreatesCache()
59
59
$ this ->assertEquals ($ authors , $ cachedResults );
60
60
$ this ->assertEmpty ($ liveResults ->diffAssoc ($ cachedResults ));
61
61
}
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
+ }
62
79
}
You can’t perform that action at this time.
0 commit comments