1
1
<?php namespace GeneaLabs \LaravelModelCaching ;
2
2
3
- use Closure ;
4
- use Illuminate \Cache \TaggableStore ;
3
+ use GeneaLabs \LaravelModelCaching \Traits \Cachable ;
4
+ use GeneaLabs \LaravelModelCaching \Traits \CacheKeyable ;
5
+ use GeneaLabs \LaravelModelCaching \Traits \CacheTagable ;
5
6
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
6
- use Illuminate \Database \Eloquent \Relations \Pivot ;
7
- use Illuminate \Support \Collection ;
8
- use Illuminate \Database \Eloquent \Relations \Relation ;
9
7
10
8
class CachedBuilder extends EloquentBuilder
11
9
{
12
- protected function cache (array $ tags = [])
13
- {
14
- $ cache = cache ();
15
-
16
- if (is_subclass_of ($ cache ->getStore (), TaggableStore::class)) {
17
- $ cache = $ cache ->tags ($ tags );
18
- }
19
-
20
- return $ cache ;
21
- }
22
-
23
- protected function getCacheKey (array $ columns = ['* ' ], $ idColumn = null ) : string
24
- {
25
- $ key = $ this ->getModelSlug ();
26
- $ key .= $ this ->getIdColumn ($ idColumn ?: '' );
27
- $ key .= $ this ->getQueryColumns ($ columns );
28
- $ key .= $ this ->getWhereClauses ();
29
- $ key .= $ this ->getWithModels ();
30
- $ key .= $ this ->getOrderByClauses ();
31
- $ key .= $ this ->getOffsetClause ();
32
- $ key .= $ this ->getLimitClause ();
33
-
34
- return $ key ;
35
- }
36
-
37
- protected function getIdColumn (string $ idColumn ) : string
38
- {
39
-
40
- return $ idColumn ? "_ {$ idColumn }" : '' ;
41
- }
42
-
43
- protected function getLimitClause () : string
44
- {
45
- if (! $ this ->query ->limit ) {
46
- return '' ;
47
- }
48
-
49
- return "-limit_ {$ this ->query ->limit }" ;
50
- }
51
-
52
- protected function getModelSlug () : string
53
- {
54
- return str_slug (get_class ($ this ->model ));
55
- }
56
-
57
- protected function getOffsetClause () : string
58
- {
59
- if (! $ this ->query ->offset ) {
60
- return '' ;
61
- }
62
-
63
- return "-offset_ {$ this ->query ->offset }" ;
64
- }
65
-
66
- protected function getQueryColumns (array $ columns ) : string
67
- {
68
- if ($ columns === ['* ' ] || $ columns === []) {
69
- return '' ;
70
- }
71
-
72
- return '_ ' . implode ('_ ' , $ columns );
73
- }
74
-
75
- protected function getWhereClauses (array $ wheres = []) : string
76
- {
77
- return $ this ->getWheres ($ wheres )
78
- ->reduce (function ($ carry , $ where ) {
79
- if (in_array ($ where ['type ' ], ['Exists ' , 'Nested ' , 'NotExists ' ])) {
80
- return '_ ' . strtolower ($ where ['type ' ]) . $ this ->getWhereClauses ($ where ['query ' ]->wheres );
81
- }
82
-
83
- if ($ where ['type ' ] === 'Column ' ) {
84
- return "_ {$ where ['boolean ' ]}_ {$ where ['first ' ]}_ {$ where ['operator ' ]}_ {$ where ['second ' ]}" ;
85
- }
86
-
87
- if ($ where ['type ' ] === 'raw ' ) {
88
- return "_ {$ where ['boolean ' ]}_ " . str_slug ($ where ['sql ' ]);
89
- }
90
-
91
- $ value = array_get ($ where , 'value ' );
92
- $ value .= $ this ->getTypeClause ($ where );
93
- $ value .= $ this ->getValuesClause ($ where );
94
-
95
- return "{$ carry }- {$ where ['column ' ]}_ {$ value }" ;
96
- })
97
- . '' ;
98
- }
99
-
100
- protected function getWithModels () : string
101
- {
102
- $ eagerLoads = collect ($ this ->eagerLoad );
103
-
104
- if ($ eagerLoads ->isEmpty ()) {
105
- return '' ;
106
- }
107
-
108
- return '- ' . implode ('- ' , $ eagerLoads ->keys ()->toArray ());
109
- }
110
-
111
- protected function getOrderByClauses (){
112
- $ orders = collect ($ this ->query ->orders );
113
-
114
- return $ orders ->reduce (function ($ carry , $ order ){
115
- return $ carry . '_orderBy_ ' . $ order ['column ' ] . '_ ' . $ order ['direction ' ];
116
- });
117
- }
118
-
119
- protected function getMethodKey (string $ postfix = null ) : string
120
- {
121
- return str_slug (get_class ($ this ->model )) . $ postfix ;
122
- }
123
-
124
- protected function getModelTag () : array
125
- {
126
- return [str_slug (get_class ($ this ->model ))];
127
- }
128
-
129
- protected function getCacheTags () : array
130
- {
131
- return collect ($ this ->eagerLoad )->keys ()
132
- ->map (function ($ relationName ) {
133
- $ relation = collect (explode ('. ' , $ relationName ))
134
- ->reduce (function ($ carry , $ name ) {
135
- if (! $ carry ) {
136
- $ carry = $ this ->model ;
137
- }
138
-
139
- if ($ carry instanceof Relation) {
140
- $ carry = $ carry ->getQuery ()->model ;
141
- }
142
-
143
- return $ carry ->{$ name }();
144
- });
145
-
146
- return str_slug (get_class ($ relation ->getQuery ()->model ));
147
- })
148
- ->prepend (str_slug (get_class ($ this ->model )))
149
- ->values ()
150
- ->toArray ();
151
- }
10
+ use Cachable;
11
+ use CacheKeyable;
12
+ use CacheTagable;
152
13
153
14
public function avg ($ column )
154
15
{
155
- return $ this ->cache ($ this ->getModelTag ())
156
- ->rememberForever ($ this ->getMethodKey ( "-avg_ {$ column }" ) , function () use ($ column ) {
16
+ return $ this ->cache ($ this ->makeCacheTags ())
17
+ ->rememberForever ($ this ->makeCacheKey () . "-avg_ {$ column }" , function () use ($ column ) {
157
18
return parent ::avg ($ column );
158
19
});
159
20
}
160
21
161
22
public function count ($ columns = ['* ' ])
162
23
{
163
- return $ this ->cache ($ this ->getModelTag ())
164
- ->rememberForever ($ this ->getMethodKey ( "-count " ) , function () use ($ columns ) {
24
+ return $ this ->cache ($ this ->makeCacheTags ())
25
+ ->rememberForever ($ this ->makeCacheKey () . "-count " , function () use ($ columns ) {
165
26
return parent ::count ($ columns );
166
27
});
167
28
}
168
29
169
30
public function cursor ()
170
31
{
171
- return $ this ->cache ($ this ->getModelTag ())
172
- ->rememberForever ($ this ->getMethodKey ( "-cursor " ) , function () {
32
+ return $ this ->cache ($ this ->makeCacheTags ())
33
+ ->rememberForever ($ this ->makeCacheKey () . "-cursor " , function () {
173
34
return collect (parent ::cursor ());
174
35
});
175
36
}
@@ -179,88 +40,63 @@ public function cursor()
179
40
*/
180
41
public function find ($ id , $ columns = ['* ' ])
181
42
{
182
- return $ this ->cache ($ this ->getCacheTags ())
183
- ->rememberForever ($ this ->getCacheKey ($ columns , $ id ), function () use ($ id , $ columns ) {
43
+ return $ this ->cache ($ this ->makeCacheTags ())
44
+ ->rememberForever ($ this ->makeCacheKey ($ columns , $ id ), function () use ($ id , $ columns ) {
184
45
return parent ::find ($ id , $ columns );
185
46
});
186
47
}
187
48
188
49
public function first ($ columns = ['* ' ])
189
50
{
190
- return $ this ->cache ($ this ->getCacheTags ())
191
- ->rememberForever ($ this ->getCacheKey ($ columns ) . '-first ' , function () use ($ columns ) {
51
+ return $ this ->cache ($ this ->makeCacheTags ())
52
+ ->rememberForever ($ this ->makeCacheKey ($ columns ) . '-first ' , function () use ($ columns ) {
192
53
return parent ::first ($ columns );
193
54
});
194
55
}
195
56
196
57
public function get ($ columns = ['* ' ])
197
58
{
198
- return $ this ->cache ($ this ->getCacheTags ())
199
- ->rememberForever ($ this ->getCacheKey ($ columns ), function () use ($ columns ) {
59
+ return $ this ->cache ($ this ->makeCacheTags ())
60
+ ->rememberForever ($ this ->makeCacheKey ($ columns ), function () use ($ columns ) {
200
61
return parent ::get ($ columns );
201
62
});
202
63
}
203
64
204
65
public function max ($ column )
205
66
{
206
- return $ this ->cache ($ this ->getModelTag ())
207
- ->rememberForever ($ this ->getMethodKey ( "-max_ {$ column }" ) , function () use ($ column ) {
67
+ return $ this ->cache ($ this ->makeCacheTags ())
68
+ ->rememberForever ($ this ->makeCacheKey () . "-max_ {$ column }" , function () use ($ column ) {
208
69
return parent ::max ($ column );
209
70
});
210
71
}
211
72
212
73
public function min ($ column )
213
74
{
214
- return $ this ->cache ($ this ->getModelTag ())
215
- ->rememberForever ($ this ->getMethodKey ( "-min_ {$ column }" ) , function () use ($ column ) {
75
+ return $ this ->cache ($ this ->makeCacheTags ())
76
+ ->rememberForever ($ this ->makeCacheKey () . "-min_ {$ column }" , function () use ($ column ) {
216
77
return parent ::min ($ column );
217
78
});
218
79
}
219
80
220
81
public function pluck ($ column , $ key = null )
221
82
{
222
- $ cacheKey = $ this ->getCacheKey ([$ column ]) . "-pluck_ {$ column }" ;
83
+ $ cacheKey = $ this ->makeCacheKey ([$ column ]) . "-pluck_ {$ column }" ;
223
84
224
85
if ($ key ) {
225
86
$ cacheKey .= "_ {$ key }" ;
226
87
}
227
88
228
- return $ this ->cache ($ this ->getCacheTags ())
89
+ return $ this ->cache ($ this ->makeCacheTags ())
229
90
->rememberForever ($ cacheKey , function () use ($ column , $ key ) {
230
91
return parent ::pluck ($ column , $ key );
231
92
});
232
93
}
233
94
234
95
public function sum ($ column )
235
96
{
236
- return $ this ->cache ($ this ->getModelTag ())
237
- ->rememberForever ($ this ->getMethodKey ( "-sum_ {$ column }" ) , function () use ($ column ) {
97
+ return $ this ->cache ($ this ->makeCacheTags ())
98
+ ->rememberForever ($ this ->makeCacheKey () . "-sum_ {$ column }" , function () use ($ column ) {
238
99
return parent ::sum ($ column );
239
100
});
240
101
}
241
-
242
- protected function getTypeClause ($ where )
243
- {
244
- return in_array ($ where ['type ' ], ['In ' , 'Null ' , 'NotNull ' ])
245
- ? strtolower ($ where ['type ' ])
246
- : '' ;
247
- }
248
-
249
- protected function getValuesClause ($ where )
250
- {
251
- return is_array (array_get ($ where , 'values ' ))
252
- ? '_ ' . implode ('_ ' , $ where ['values ' ])
253
- : '' ;
254
- }
255
-
256
- protected function getWheres (array $ wheres ) : Collection
257
- {
258
- $ wheres = collect ($ wheres );
259
-
260
- if ($ wheres ->isEmpty ()) {
261
- $ wheres = collect ($ this ->query ->wheres );
262
- }
263
-
264
- return $ wheres ;
265
- }
266
102
}
0 commit comments