33use GeneaLabs \LaravelModelCaching \Traits \Cachable ;
44use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
55
6+ /**
7+ * @SuppressWarnings(PHPMD.TooManyPublicMethods)
8+ */
69class 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 );
0 commit comments