|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\services\cache; |
|
3
|
|
|
|
|
4
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class TransientCacheStorage |
|
10
|
|
|
* Manages the creation and cleanup of transients |
|
11
|
|
|
* by tracking transient keys and their corresponding expiration. |
|
12
|
|
|
* The transient cleanup schedule is filterable |
|
13
|
|
|
* to control how often cleanup occurs |
|
14
|
|
|
* |
|
15
|
|
|
* @package Event Espresso |
|
16
|
|
|
* @author Brent Christensen |
|
17
|
|
|
* @since 4.9.31 |
|
18
|
|
|
*/ |
|
19
|
|
|
class TransientCacheStorage implements CacheStorageInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* wp-option option_name for tracking transients |
|
24
|
|
|
* |
|
25
|
|
|
* @type string |
|
26
|
|
|
*/ |
|
27
|
|
|
const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int $current_time |
|
31
|
|
|
*/ |
|
32
|
|
|
private $current_time; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* how often to perform transient cleanup |
|
36
|
|
|
* |
|
37
|
|
|
* @var string $transient_cleanup_frequency |
|
38
|
|
|
*/ |
|
39
|
|
|
private $transient_cleanup_frequency; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* options for how often to perform transient cleanup |
|
43
|
|
|
* |
|
44
|
|
|
* @var array $transient_cleanup_frequency_options |
|
45
|
|
|
*/ |
|
46
|
|
|
private $transient_cleanup_frequency_options = array(); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array $transients |
|
50
|
|
|
*/ |
|
51
|
|
|
private $transients; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* TransientCacheStorage constructor. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
61
|
|
|
// round current time down to closest 5 minutes to simplify scheduling |
|
62
|
|
|
$this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
63
|
|
|
$this->transients = (array)get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
64
|
|
|
if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
65
|
|
|
add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets how often transient cleanup occurs |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
private function setTransientCleanupFrequency() |
|
77
|
|
|
{ |
|
78
|
|
|
// sets how often transients are cleaned up |
|
79
|
|
|
$this->transient_cleanup_frequency_options = apply_filters( |
|
80
|
|
|
'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
81
|
|
|
array( |
|
82
|
|
|
'off', |
|
83
|
|
|
'15-minutes', |
|
84
|
|
|
'hour', |
|
85
|
|
|
'12-hours', |
|
86
|
|
|
'day', |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
$transient_cleanup_frequency = apply_filters( |
|
90
|
|
|
'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
91
|
|
|
'hour' |
|
92
|
|
|
); |
|
93
|
|
|
return in_array( |
|
94
|
|
|
$transient_cleanup_frequency, |
|
95
|
|
|
$this->transient_cleanup_frequency_options, |
|
96
|
|
|
true |
|
97
|
|
|
) |
|
98
|
|
|
? $transient_cleanup_frequency |
|
99
|
|
|
: 'hour'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* we need to be able to round timestamps off to match the set transient cleanup frequency |
|
106
|
|
|
* so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
107
|
|
|
* then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
108
|
|
|
* during the next scheduled cleanup after its expiration. |
|
109
|
|
|
* We also round off the current time timestamp to the closest 5 minutes |
|
110
|
|
|
* just to make the timestamps a little easier to round which helps with debugging. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $timestamp [required] |
|
113
|
|
|
* @param string $cleanup_frequency |
|
114
|
|
|
* @param bool $round_up |
|
115
|
|
|
* @return int |
|
116
|
|
|
*/ |
|
117
|
|
|
private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
118
|
|
|
{ |
|
119
|
|
|
$cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
120
|
|
|
// in order to round the time to the closest xx minutes (or hours), |
|
121
|
|
|
// we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
122
|
|
|
// round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
123
|
|
|
// why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
124
|
|
|
// and bump the hour, which could bump the day, which could bump the month, etc, |
|
125
|
|
|
// which would be bad because we don't always want to round up, |
|
126
|
|
|
// but when we do we can easily achieve that by simply adding the desired offset, |
|
127
|
|
|
$minutes = '00'; |
|
128
|
|
|
$hours = 'H'; |
|
129
|
|
|
switch ($cleanup_frequency) { |
|
130
|
|
View Code Duplication |
case '5-minutes' : |
|
131
|
|
|
$minutes = floor((int)date('i', $timestamp) / 5) * 5; |
|
132
|
|
|
$minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
133
|
|
|
$offset = MINUTE_IN_SECONDS * 5; |
|
134
|
|
|
break; |
|
135
|
|
View Code Duplication |
case '15-minutes' : |
|
136
|
|
|
$minutes = floor((int)date('i', $timestamp) / 15) * 15; |
|
137
|
|
|
$minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
138
|
|
|
$offset = MINUTE_IN_SECONDS * 15; |
|
139
|
|
|
break; |
|
140
|
|
View Code Duplication |
case '12-hours' : |
|
141
|
|
|
$hours = floor((int)date('H', $timestamp) / 12) * 12; |
|
142
|
|
|
$hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
143
|
|
|
$offset = HOUR_IN_SECONDS * 12; |
|
144
|
|
|
break; |
|
145
|
|
|
case 'day' : |
|
146
|
|
|
$hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
147
|
|
|
$offset = DAY_IN_SECONDS; |
|
148
|
|
|
break; |
|
149
|
|
|
case 'hour' : |
|
150
|
|
|
default : |
|
151
|
|
|
$offset = HOUR_IN_SECONDS; |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
$rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
155
|
|
|
$rounded_timestamp += $round_up ? $offset : 0; |
|
156
|
|
|
return apply_filters( |
|
157
|
|
|
'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
158
|
|
|
$rounded_timestamp, |
|
159
|
|
|
$timestamp, |
|
160
|
|
|
$cleanup_frequency, |
|
161
|
|
|
$round_up |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Saves supplied data to a transient |
|
169
|
|
|
* if an expiration is set, then it automatically schedules the transient for cleanup |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $transient_key [required] |
|
172
|
|
|
* @param string $data [required] |
|
173
|
|
|
* @param int $expiration number of seconds until the cache expires |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
public function add($transient_key, $data, $expiration = 0) |
|
177
|
|
|
{ |
|
178
|
|
|
$expiration = (int)abs($expiration); |
|
179
|
|
|
$saved = set_transient($transient_key, $data, $expiration); |
|
180
|
|
|
if ($saved && $expiration) { |
|
181
|
|
|
$this->scheduleTransientCleanup($transient_key, $expiration); |
|
182
|
|
|
} |
|
183
|
|
|
return $saved; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* retrieves transient data |
|
190
|
|
|
* automatically triggers early cache refresh for standard cache items |
|
191
|
|
|
* in order to avoid cache stampedes on busy sites. |
|
192
|
|
|
* For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
193
|
|
|
* the $standard_cache parameter should be set to false when retrieving data |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $transient_key [required] |
|
196
|
|
|
* @param bool $standard_cache |
|
197
|
|
|
* @return mixed|null |
|
198
|
|
|
*/ |
|
199
|
|
|
public function get($transient_key, $standard_cache = true) |
|
200
|
|
|
{ |
|
201
|
|
|
// to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
202
|
|
|
// check if known cache expires within the next minute, |
|
203
|
|
|
// and if so, remove it from our tracking and and return nothing. |
|
204
|
|
|
// this should trigger the cache content to be regenerated during this request, |
|
205
|
|
|
// while allowing any following requests to still access the existing cache |
|
206
|
|
|
// until it gets replaced with the refreshed content |
|
207
|
|
|
if ( |
|
208
|
|
|
$standard_cache |
|
209
|
|
|
&& isset($this->transients[$transient_key]) |
|
210
|
|
|
&& $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
|
211
|
|
|
) { |
|
212
|
|
|
unset($this->transients[$transient_key]); |
|
213
|
|
|
$this->updateTransients(); |
|
214
|
|
|
return null; |
|
215
|
|
|
} |
|
216
|
|
|
$content = get_transient($transient_key); |
|
217
|
|
|
return $content !== false ? $content : null; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* delete a single transient and remove tracking |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $transient_key [required] full or partial transient key to be deleted |
|
226
|
|
|
*/ |
|
227
|
|
|
public function delete($transient_key) |
|
228
|
|
|
{ |
|
229
|
|
|
$this->deleteMany(array($transient_key)); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* delete multiple transients and remove tracking |
|
236
|
|
|
* |
|
237
|
|
|
* @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
238
|
|
|
* @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
239
|
|
|
* and proceed directly to deleting those entries from the cache storage |
|
240
|
|
|
*/ |
|
241
|
|
|
public function deleteMany(array $transient_keys, $force_delete = false) |
|
242
|
|
|
{ |
|
243
|
|
|
$full_transient_keys = $force_delete ? $transient_keys : array(); |
|
244
|
|
|
if(empty($full_transient_keys)){ |
|
245
|
|
|
foreach ($this->transients as $transient_key => $expiration) { |
|
246
|
|
|
foreach ($transient_keys as $transient_key_to_delete) { |
|
247
|
|
|
if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
248
|
|
|
$full_transient_keys[] = $transient_key; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
if ($this->deleteTransientKeys($full_transient_keys)) { |
|
254
|
|
|
$this->updateTransients(); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* sorts transients numerically by timestamp |
|
262
|
|
|
* then saves the transient schedule to a WP option |
|
263
|
|
|
*/ |
|
264
|
|
|
private function updateTransients() |
|
265
|
|
|
{ |
|
266
|
|
|
asort($this->transients, SORT_NUMERIC); |
|
267
|
|
|
update_option( |
|
268
|
|
|
TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
269
|
|
|
$this->transients |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* schedules a transient for cleanup by adding it to the transient tracking |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $transient_key [required] |
|
279
|
|
|
* @param int $expiration [required] |
|
280
|
|
|
*/ |
|
281
|
|
|
private function scheduleTransientCleanup($transient_key, $expiration) |
|
282
|
|
|
{ |
|
283
|
|
|
// make sure a valid future timestamp is set |
|
284
|
|
|
$expiration += $expiration < time() ? time() : 0; |
|
285
|
|
|
// and round to the closest 15 minutes |
|
286
|
|
|
$expiration = $this->roundTimestamp($expiration); |
|
287
|
|
|
// save transients to clear using their ID as the key to avoid duplicates |
|
288
|
|
|
$this->transients[$transient_key] = $expiration; |
|
289
|
|
|
$this->updateTransients(); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Since our tracked transients are sorted by their timestamps |
|
296
|
|
|
* we can grab the first transient and see when it is scheduled for cleanup. |
|
297
|
|
|
* If that timestamp is less than or equal to the current time, |
|
298
|
|
|
* then cleanup is triggered |
|
299
|
|
|
*/ |
|
300
|
|
|
public function checkTransientCleanupSchedule() |
|
301
|
|
|
{ |
|
302
|
|
|
if (empty($this->transients)) { |
|
303
|
|
|
return; |
|
304
|
|
|
} |
|
305
|
|
|
// when do we run the next cleanup job? |
|
306
|
|
|
reset($this->transients); |
|
307
|
|
|
$next_scheduled_cleanup = current($this->transients); |
|
308
|
|
|
// if the next cleanup job is scheduled for the current hour |
|
309
|
|
|
if ($next_scheduled_cleanup <= $this->current_time) { |
|
310
|
|
|
if ($this->cleanupExpiredTransients()) { |
|
311
|
|
|
$this->updateTransients(); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* loops through the array of tracked transients, |
|
320
|
|
|
* compiles a list of those that have expired, and sends that list off for deletion. |
|
321
|
|
|
* Also removes any bad records from the transients array |
|
322
|
|
|
* |
|
323
|
|
|
* @return bool |
|
324
|
|
|
*/ |
|
325
|
|
|
private function cleanupExpiredTransients() |
|
326
|
|
|
{ |
|
327
|
|
|
$update = false; |
|
328
|
|
|
// filter the query limit. Set to 0 to turn off garbage collection |
|
329
|
|
|
$limit = (int)abs( |
|
330
|
|
|
apply_filters( |
|
331
|
|
|
'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
332
|
|
|
50 |
|
333
|
|
|
) |
|
334
|
|
|
); |
|
335
|
|
|
// non-zero LIMIT means take out the trash |
|
336
|
|
|
if ($limit) { |
|
337
|
|
|
$transient_keys = array(); |
|
338
|
|
|
foreach ($this->transients as $transient_key => $expiration) { |
|
339
|
|
|
if ($expiration > $this->current_time) { |
|
340
|
|
|
continue; |
|
341
|
|
|
} |
|
342
|
|
|
if ( ! $expiration || ! $transient_key) { |
|
343
|
|
|
unset($this->transients[$transient_key]); |
|
344
|
|
|
$update = true; |
|
345
|
|
|
continue; |
|
346
|
|
|
} |
|
347
|
|
|
$transient_keys[] = $transient_key; |
|
348
|
|
|
} |
|
349
|
|
|
// delete expired keys, but maintain value of $update if nothing is deleted |
|
350
|
|
|
$update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
351
|
|
|
do_action( 'FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
352
|
|
|
} |
|
353
|
|
|
return $update; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* calls delete_transient() on each transient key provided, up to the specified limit |
|
360
|
|
|
* |
|
361
|
|
|
* @param array $transient_keys [required] |
|
362
|
|
|
* @param int $limit |
|
363
|
|
|
* @return bool |
|
364
|
|
|
*/ |
|
365
|
|
|
private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
366
|
|
|
{ |
|
367
|
|
|
if (empty($transient_keys)) { |
|
368
|
|
|
return false; |
|
369
|
|
|
} |
|
370
|
|
|
$counter = 0; |
|
371
|
|
|
foreach ($transient_keys as $transient_key) { |
|
372
|
|
|
if($counter === $limit){ |
|
373
|
|
|
break; |
|
374
|
|
|
} |
|
375
|
|
|
// remove any transient prefixes |
|
376
|
|
|
$transient_key = strpos($transient_key, '_transient_timeout_') === 0 |
|
377
|
|
|
? str_replace('_transient_timeout_', '', $transient_key) |
|
378
|
|
|
: $transient_key; |
|
379
|
|
|
$transient_key = strpos($transient_key, '_transient_') === 0 |
|
380
|
|
|
? str_replace('_transient_', '', $transient_key) |
|
381
|
|
|
: $transient_key; |
|
382
|
|
|
if(delete_transient($transient_key)){ |
|
383
|
|
|
unset($this->transients[$transient_key]); |
|
384
|
|
|
$counter++; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
return $counter > 0; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
|
|
391
|
|
|
|
|
392
|
|
|
} |
|
393
|
|
|
// End of file TransientCacheStorage.php |
|
394
|
|
|
// Location: EventEspresso\core\services\cache/TransientCacheStorage.php |
|
395
|
|
|
|