@@ -16,13 +16,11 @@ public function avg($column)
16
16
return parent ::avg ($ column );
17
17
}
18
18
19
- return $ this ->cache ($ this ->makeCacheTags ())
20
- ->rememberForever (
21
- $ this ->makeCacheKey (['* ' ], null , "-avg_ {$ column }" ),
22
- function () use ($ column ) {
23
- return parent ::avg ($ column );
24
- }
25
- );
19
+ $ arguments = func_get_args ();
20
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-avg_ {$ column }" );
21
+ $ method = 'avg ' ;
22
+
23
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
26
24
}
27
25
28
26
public function count ($ columns = ['* ' ])
@@ -31,13 +29,11 @@ public function count($columns = ['*'])
31
29
return parent ::count ($ columns );
32
30
}
33
31
34
- return $ this ->cache ($ this ->makeCacheTags ())
35
- ->rememberForever (
36
- $ this ->makeCacheKey (['* ' ], null , "-count " ),
37
- function () use ($ columns ) {
38
- return parent ::count ($ columns );
39
- }
40
- );
32
+ $ arguments = func_get_args ();
33
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-count " );
34
+ $ method = 'count ' ;
35
+
36
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
41
37
}
42
38
43
39
public function cursor ()
@@ -46,13 +42,11 @@ public function cursor()
46
42
return collect (parent ::cursor ());
47
43
}
48
44
49
- return $ this ->cache ($ this ->makeCacheTags ())
50
- ->rememberForever (
51
- $ this ->makeCacheKey (['* ' ], null , "-cursor " ),
52
- function () {
53
- return collect (parent ::cursor ());
54
- }
55
- );
45
+ $ arguments = func_get_args ();
46
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-cursor " );
47
+ $ method = 'cursor ' ;
48
+
49
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
56
50
}
57
51
58
52
public function delete ()
@@ -72,13 +66,11 @@ public function find($id, $columns = ['*'])
72
66
return parent ::find ($ id , $ columns );
73
67
}
74
68
75
- return $ this ->cache ($ this ->makeCacheTags ())
76
- ->rememberForever (
77
- $ this ->makeCacheKey ($ columns , $ id ),
78
- function () use ($ id , $ columns ) {
79
- return parent ::find ($ id , $ columns );
80
- }
81
- );
69
+ $ arguments = func_get_args ();
70
+ $ cacheKey = $ this ->makeCacheKey ($ columns );
71
+ $ method = 'find ' ;
72
+
73
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
82
74
}
83
75
84
76
public function first ($ columns = ['* ' ])
@@ -87,13 +79,11 @@ public function first($columns = ['*'])
87
79
return parent ::first ($ columns );
88
80
}
89
81
90
- return $ this ->cache ($ this ->makeCacheTags ())
91
- ->rememberForever (
92
- $ this ->makeCacheKey ($ columns , null , '-first ' ),
93
- function () use ($ columns ) {
94
- return parent ::first ($ columns );
95
- }
96
- );
82
+ $ arguments = func_get_args ();
83
+ $ cacheKey = $ this ->makeCacheKey ($ columns );
84
+ $ method = 'first ' ;
85
+
86
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
97
87
}
98
88
99
89
public function get ($ columns = ['* ' ])
@@ -102,13 +92,11 @@ public function get($columns = ['*'])
102
92
return parent ::get ($ columns );
103
93
}
104
94
105
- return $ this ->cache ($ this ->makeCacheTags ())
106
- ->rememberForever (
107
- $ this ->makeCacheKey ($ columns ),
108
- function () use ($ columns ) {
109
- return parent ::get ($ columns );
110
- }
111
- );
95
+ $ arguments = func_get_args ();
96
+ $ cacheKey = $ this ->makeCacheKey ($ columns );
97
+ $ method = 'get ' ;
98
+
99
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
112
100
}
113
101
114
102
public function max ($ column )
@@ -117,13 +105,11 @@ public function max($column)
117
105
return parent ::max ($ column );
118
106
}
119
107
120
- return $ this ->cache ($ this ->makeCacheTags ())
121
- ->rememberForever (
122
- $ this ->makeCacheKey (['* ' ], null , "-max_ {$ column }" ),
123
- function () use ($ column ) {
124
- return parent ::max ($ column );
125
- }
126
- );
108
+ $ arguments = func_get_args ();
109
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-max_ {$ column }" );
110
+ $ method = 'max ' ;
111
+
112
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
127
113
}
128
114
129
115
public function min ($ column )
@@ -132,13 +118,11 @@ public function min($column)
132
118
return parent ::min ($ column );
133
119
}
134
120
135
- return $ this ->cache ($ this ->makeCacheTags ())
136
- ->rememberForever (
137
- $ this ->makeCacheKey (['* ' ], null , "-min_ {$ column }" ),
138
- function () use ($ column ) {
139
- return parent ::min ($ column );
140
- }
141
- );
121
+ $ arguments = func_get_args ();
122
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-min_ {$ column }" );
123
+ $ method = 'min ' ;
124
+
125
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
142
126
}
143
127
144
128
public function pluck ($ column , $ key = null )
@@ -148,12 +132,11 @@ public function pluck($column, $key = null)
148
132
}
149
133
150
134
$ keyDifferentiator = "-pluck_ {$ column }" . ($ key ? "_ {$ key }" : "" );
135
+ $ arguments = func_get_args ();
151
136
$ cacheKey = $ this ->makeCacheKey ([$ column ], null , $ keyDifferentiator );
137
+ $ method = 'pluck ' ;
152
138
153
- return $ this ->cache ($ this ->makeCacheTags ())
154
- ->rememberForever ($ cacheKey , function () use ($ column , $ key ) {
155
- return parent ::pluck ($ column , $ key );
156
- });
139
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
157
140
}
158
141
159
142
public function sum ($ column )
@@ -162,13 +145,11 @@ public function sum($column)
162
145
return parent ::sum ($ column );
163
146
}
164
147
165
- return $ this ->cache ($ this ->makeCacheTags ())
166
- ->rememberForever (
167
- $ this ->makeCacheKey (['* ' ], null , "-sum_ {$ column }" ),
168
- function () use ($ column ) {
169
- return parent ::sum ($ column );
170
- }
171
- );
148
+ $ arguments = func_get_args ();
149
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-sum_ {$ column }" );
150
+ $ method = 'sum ' ;
151
+
152
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
172
153
}
173
154
174
155
public function value ($ column )
@@ -177,11 +158,56 @@ public function value($column)
177
158
return parent ::value ($ column );
178
159
}
179
160
180
- return $ this ->cache ($ this ->makeCacheTags ())
161
+ $ arguments = func_get_args ();
162
+ $ cacheKey = $ this ->makeCacheKey (['* ' ], null , "-value_ {$ column }" );
163
+ $ method = 'value ' ;
164
+
165
+ return $ this ->cachedValue ($ arguments , $ cacheKey , $ method );
166
+ }
167
+
168
+ public function cachedValue (array $ arguments , string $ cacheKey , string $ method )
169
+ {
170
+ $ cacheTags = $ this ->makeCacheTags ();
171
+ $ hashedCacheKey = sha1 ($ cacheKey );
172
+
173
+ $ result = $ this ->retrieveCachedValue (
174
+ $ arguments ,
175
+ $ cacheKey ,
176
+ $ cacheTags ,
177
+ $ hashedCacheKey ,
178
+ $ method
179
+ );
180
+
181
+ if ($ result ['key ' ] !== $ cacheKey ) {
182
+ cache ()->tags ($ cacheTags )->forget ($ hashedCacheKey );
183
+ }
184
+
185
+ $ result = $ this ->retrieveCachedValue (
186
+ $ arguments ,
187
+ $ cacheKey ,
188
+ $ cacheTags ,
189
+ $ hashedCacheKey ,
190
+ $ method
191
+ );
192
+
193
+ return $ result ['value ' ];
194
+ }
195
+
196
+ protected function retrieveCachedValue (
197
+ array $ arguments ,
198
+ string $ cacheKey ,
199
+ array $ cacheTags ,
200
+ string $ hashedCacheKey ,
201
+ string $ method
202
+ ) {
203
+ return $ this ->cache ($ cacheTags )
181
204
->rememberForever (
182
- $ this ->makeCacheKey (['* ' ], null , "-value_ {$ column }" ),
183
- function () use ($ column ) {
184
- return parent ::value ($ column );
205
+ $ hashedCacheKey ,
206
+ function () use ($ arguments , $ cacheKey , $ method ) {
207
+ return [
208
+ 'key ' => $ cacheKey ,
209
+ 'value ' => parent ::{$ method }(...$ arguments ),
210
+ ];
185
211
}
186
212
);
187
213
}
0 commit comments