3
3
use GeneaLabs \LaravelModelCaching \Traits \Cachable ;
4
4
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
5
5
6
+ /**
7
+ * @SuppressWarnings(PHPMD.TooManyPublicMethods)
8
+ */
6
9
class CachedBuilder extends EloquentBuilder
7
10
{
8
11
use Cachable;
9
12
13
+ protected $ isCachable = true ;
14
+
10
15
public function avg ($ column )
11
16
{
17
+ if (! $ this ->isCachable ) {
18
+ return parent ::avg ($ column );
19
+ }
20
+
12
21
return $ this ->cache ($ this ->makeCacheTags ())
13
22
->rememberForever ($ this ->makeCacheKey () . "-avg_ {$ column }" , function () use ($ column ) {
14
23
return parent ::avg ($ column );
@@ -17,6 +26,10 @@ public function avg($column)
17
26
18
27
public function count ($ columns = ['* ' ])
19
28
{
29
+ if (! $ this ->isCachable ) {
30
+ return parent ::count ($ columns );
31
+ }
32
+
20
33
return $ this ->cache ($ this ->makeCacheTags ())
21
34
->rememberForever ($ this ->makeCacheKey () . "-count " , function () use ($ columns ) {
22
35
return parent ::count ($ columns );
@@ -25,6 +38,10 @@ public function count($columns = ['*'])
25
38
26
39
public function cursor ()
27
40
{
41
+ if (! $ this ->isCachable ) {
42
+ return collect (parent ::cursor ());
43
+ }
44
+
28
45
return $ this ->cache ($ this ->makeCacheTags ())
29
46
->rememberForever ($ this ->makeCacheKey () . "-cursor " , function () {
30
47
return collect (parent ::cursor ());
@@ -39,11 +56,22 @@ public function delete()
39
56
return parent ::delete ();
40
57
}
41
58
59
+ public function disableCache ()
60
+ {
61
+ $ this ->isCachable = false ;
62
+
63
+ return $ this ;
64
+ }
65
+
42
66
/**
43
67
* @SuppressWarnings(PHPMD.ShortVariable)
44
68
*/
45
69
public function find ($ id , $ columns = ['* ' ])
46
70
{
71
+ if (! $ this ->isCachable ) {
72
+ return parent ::find ($ id , $ columns );
73
+ }
74
+
47
75
return $ this ->cache ($ this ->makeCacheTags ())
48
76
->rememberForever ($ this ->makeCacheKey ($ columns , $ id ), function () use ($ id , $ columns ) {
49
77
return parent ::find ($ id , $ columns );
@@ -52,6 +80,10 @@ public function find($id, $columns = ['*'])
52
80
53
81
public function first ($ columns = ['* ' ])
54
82
{
83
+ if (! $ this ->isCachable ) {
84
+ return parent ::first ($ columns );
85
+ }
86
+
55
87
return $ this ->cache ($ this ->makeCacheTags ())
56
88
->rememberForever ($ this ->makeCacheKey ($ columns ) . '-first ' , function () use ($ columns ) {
57
89
return parent ::first ($ columns );
@@ -60,6 +92,10 @@ public function first($columns = ['*'])
60
92
61
93
public function get ($ columns = ['* ' ])
62
94
{
95
+ if (! $ this ->isCachable ) {
96
+ return parent ::get ($ columns );
97
+ }
98
+
63
99
return $ this ->cache ($ this ->makeCacheTags ())
64
100
->rememberForever ($ this ->makeCacheKey ($ columns ), function () use ($ columns ) {
65
101
return parent ::get ($ columns );
@@ -68,6 +104,10 @@ public function get($columns = ['*'])
68
104
69
105
public function max ($ column )
70
106
{
107
+ if (! $ this ->isCachable ) {
108
+ return parent ::max ($ column );
109
+ }
110
+
71
111
return $ this ->cache ($ this ->makeCacheTags ())
72
112
->rememberForever ($ this ->makeCacheKey () . "-max_ {$ column }" , function () use ($ column ) {
73
113
return parent ::max ($ column );
@@ -76,6 +116,10 @@ public function max($column)
76
116
77
117
public function min ($ column )
78
118
{
119
+ if (! $ this ->isCachable ) {
120
+ return parent ::min ($ column );
121
+ }
122
+
79
123
return $ this ->cache ($ this ->makeCacheTags ())
80
124
->rememberForever ($ this ->makeCacheKey () . "-min_ {$ column }" , function () use ($ column ) {
81
125
return parent ::min ($ column );
@@ -84,6 +128,10 @@ public function min($column)
84
128
85
129
public function pluck ($ column , $ key = null )
86
130
{
131
+ if (! $ this ->isCachable ) {
132
+ return parent ::pluck ($ column , $ key );
133
+ }
134
+
87
135
$ cacheKey = $ this ->makeCacheKey ([$ column ]) . "-pluck_ {$ column }" ;
88
136
89
137
if ($ key ) {
@@ -98,6 +146,10 @@ public function pluck($column, $key = null)
98
146
99
147
public function sum ($ column )
100
148
{
149
+ if (! $ this ->isCachable ) {
150
+ return parent ::sum ($ column );
151
+ }
152
+
101
153
return $ this ->cache ($ this ->makeCacheTags ())
102
154
->rememberForever ($ this ->makeCacheKey () . "-sum_ {$ column }" , function () use ($ column ) {
103
155
return parent ::sum ($ column );
0 commit comments