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
13+ protected $ isCachable = true ;
14+
1015 public function avg ($ column )
1116 {
17+ if (! $ this ->isCachable ) {
18+ return parent ::avg ($ column );
19+ }
20+
1221 return $ this ->cache ($ this ->makeCacheTags ())
1322 ->rememberForever ($ this ->makeCacheKey () . "-avg_ {$ column }" , function () use ($ column ) {
1423 return parent ::avg ($ column );
@@ -17,6 +26,10 @@ public function avg($column)
1726
1827 public function count ($ columns = ['* ' ])
1928 {
29+ if (! $ this ->isCachable ) {
30+ return parent ::count ($ columns );
31+ }
32+
2033 return $ this ->cache ($ this ->makeCacheTags ())
2134 ->rememberForever ($ this ->makeCacheKey () . "-count " , function () use ($ columns ) {
2235 return parent ::count ($ columns );
@@ -25,6 +38,10 @@ public function count($columns = ['*'])
2538
2639 public function cursor ()
2740 {
41+ if (! $ this ->isCachable ) {
42+ return collect (parent ::cursor ());
43+ }
44+
2845 return $ this ->cache ($ this ->makeCacheTags ())
2946 ->rememberForever ($ this ->makeCacheKey () . "-cursor " , function () {
3047 return collect (parent ::cursor ());
@@ -39,11 +56,22 @@ public function delete()
3956 return parent ::delete ();
4057 }
4158
59+ public function disableCache ()
60+ {
61+ $ this ->isCachable = false ;
62+
63+ return $ this ;
64+ }
65+
4266 /**
4367 * @SuppressWarnings(PHPMD.ShortVariable)
4468 */
4569 public function find ($ id , $ columns = ['* ' ])
4670 {
71+ if (! $ this ->isCachable ) {
72+ return parent ::find ($ id , $ columns );
73+ }
74+
4775 return $ this ->cache ($ this ->makeCacheTags ())
4876 ->rememberForever ($ this ->makeCacheKey ($ columns , $ id ), function () use ($ id , $ columns ) {
4977 return parent ::find ($ id , $ columns );
@@ -52,6 +80,10 @@ public function find($id, $columns = ['*'])
5280
5381 public function first ($ columns = ['* ' ])
5482 {
83+ if (! $ this ->isCachable ) {
84+ return parent ::first ($ columns );
85+ }
86+
5587 return $ this ->cache ($ this ->makeCacheTags ())
5688 ->rememberForever ($ this ->makeCacheKey ($ columns ) . '-first ' , function () use ($ columns ) {
5789 return parent ::first ($ columns );
@@ -60,6 +92,10 @@ public function first($columns = ['*'])
6092
6193 public function get ($ columns = ['* ' ])
6294 {
95+ if (! $ this ->isCachable ) {
96+ return parent ::get ($ columns );
97+ }
98+
6399 return $ this ->cache ($ this ->makeCacheTags ())
64100 ->rememberForever ($ this ->makeCacheKey ($ columns ), function () use ($ columns ) {
65101 return parent ::get ($ columns );
@@ -68,6 +104,10 @@ public function get($columns = ['*'])
68104
69105 public function max ($ column )
70106 {
107+ if (! $ this ->isCachable ) {
108+ return parent ::max ($ column );
109+ }
110+
71111 return $ this ->cache ($ this ->makeCacheTags ())
72112 ->rememberForever ($ this ->makeCacheKey () . "-max_ {$ column }" , function () use ($ column ) {
73113 return parent ::max ($ column );
@@ -76,6 +116,10 @@ public function max($column)
76116
77117 public function min ($ column )
78118 {
119+ if (! $ this ->isCachable ) {
120+ return parent ::min ($ column );
121+ }
122+
79123 return $ this ->cache ($ this ->makeCacheTags ())
80124 ->rememberForever ($ this ->makeCacheKey () . "-min_ {$ column }" , function () use ($ column ) {
81125 return parent ::min ($ column );
@@ -84,6 +128,10 @@ public function min($column)
84128
85129 public function pluck ($ column , $ key = null )
86130 {
131+ if (! $ this ->isCachable ) {
132+ return parent ::pluck ($ column , $ key );
133+ }
134+
87135 $ cacheKey = $ this ->makeCacheKey ([$ column ]) . "-pluck_ {$ column }" ;
88136
89137 if ($ key ) {
@@ -98,6 +146,10 @@ public function pluck($column, $key = null)
98146
99147 public function sum ($ column )
100148 {
149+ if (! $ this ->isCachable ) {
150+ return parent ::sum ($ column );
151+ }
152+
101153 return $ this ->cache ($ this ->makeCacheTags ())
102154 ->rememberForever ($ this ->makeCacheKey () . "-sum_ {$ column }" , function () use ($ column ) {
103155 return parent ::sum ($ column );
0 commit comments