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