Jake Vanderwerf
2025-11-04 42fa8304ddb811b0f725f245130f70c0f5e86a6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
namespace JVBase\managers;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class CacheManagerOld
{
    private string $prefix = 'jvb_';
    private string $group;
    private int $cache_ttl;
    private static ?bool $use_object_cache = null;
 
    /**
     * @param string|null $group The group name for this cache instance
     * @param int|null $ttl The default ttl for this instance
     */
    public function __construct(?string $group = null, ?int $ttl = null)
    {
        $this->group = $group ?: 'jvb_default';
        $this->cache_ttl = $ttl ?: 3600;
 
        // Check if Redis/Memcached is available
        if (is_null(static::$use_object_cache)) {
            static::$use_object_cache = !is_null(wp_using_ext_object_cache());
//          error_log((static::$use_object_cache) ? 'Using Object Cache' : 'Not using Object Cache');
        }
    }
 
    /**
     * Get a value from the cache
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param string|null $group The group to get from. Defaults to current group
     * @return mixed
     */
    public function get(string|array $key, ?string $group = null): mixed
    {
        $group = $group ?: $this->group;
 
        $key = $this->normalizeKey($key);
 
        $cache_key = $this->buildKey($key);
 
        // Use appropriate cache method
        if (static::$use_object_cache) {
            $value = wp_cache_get($cache_key, $group);
        } else {
            // Fallback to transients for local development
            $value = get_transient($this->getTransientKey($cache_key, $group));
        }
 
        return (is_array($value) && array_key_exists('data', $value)) ? $value['data'] : $value;
    }
 
    public function getTimestamp(string|array $key, ?string $group = null): mixed
    {
        $group = $group ?: $this->group;
 
        $key = $this->normalizeKey($key);
 
        $cache_key = $this->buildKey($key);
 
        // Use appropriate cache method
        if (static::$use_object_cache) {
            $value = wp_cache_get($cache_key, $group);
        } else {
            // Fallback to transients for local development
            $value = get_transient($this->getTransientKey($cache_key, $group));
        }
 
        return (is_array($value) && array_key_exists('last_modified', $value)) ? $value['last_modified'] : false;
    }
 
    /**
     * Store a value in cache
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param mixed $value The Value to set
     * @param int|null $ttl The ttl (defaults to current set ttl)
     * @param string|null $group The group to add cache to (defaults to current group))
     * @return bool
     */
    public function set(string|array $key, mixed $value, ?int $ttl = null, ?string $group = null): bool
    {
        $ttl = $ttl ?: $this->cache_ttl;
        $group = $group ?: $this->group;
 
        $key = $this->normalizeKey($key);
 
        $cache_key = $this->buildKey($key);
        $temp = [
            'data' => $value,
            'last_modified' => time(),
        ];
        $value = $temp;
 
        // Use appropriate cache method
        if (static::$use_object_cache) {
            return wp_cache_set($cache_key, $value, $group, $ttl);
        } else {
            // Fallback to transients
            return set_transient($this->getTransientKey($cache_key, $group), $value, $ttl);
        }
    }
 
    /**
     * Delete a cached value
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param string|null $group The group to delete from (defaults to current group)
     * @return bool
     */
    public function delete(string|array $key, ?string $group = null): bool
    {
        $group = $group ?: $this->group;
 
        $key = $this->normalizeKey($key);
 
        $cache_key = $this->buildKey($key);
 
        // Use appropriate cache method
        if (static::$use_object_cache) {
            return wp_cache_delete($cache_key, $group);
        } else {
            return delete_transient($this->getTransientKey($cache_key, $group));
        }
    }
 
    public function clear():bool
    {
        try {
            if (static::$use_object_cache) {
                // With Redis, this could be implemented with SCAN command
                // but wp_cache_* doesn't expose this, so we'd need direct Redis access
                // For now, just flush the group as a nuclear option
                if (function_exists('wp_cache_flush_group')) {
                    wp_cache_flush_group($this->group);
                    return true;
                }
                return false;
            } else {
                // For transients, search and delete
                global $wpdb;
 
                $prefix = self::getTransientPrefix($this->group);
                $sql = "SELECT option_name FROM {$wpdb->options}
                    WHERE option_name LIKE %s
                    AND option_name LIKE %s";
 
                $keys = $wpdb->get_col($wpdb->prepare(
                    $sql,
                    '_transient_' . $prefix . '%'
                ));
 
                foreach ($keys as $key) {
                    $transient_key = str_replace('_transient_', '', $key);
                    delete_transient($transient_key);
                }
                return true;
            }
        } catch (\Exception $e) {
 
        } finally {
            return false;
        }
    }
 
    /**
     * Alias for delete() for backwards compatibility
     * @param string $key The key to look up (auto-generates key from array of key=>values)
     * @param string|null $group The group to delete from (defaults to current group))
     * @return void
     */
    public function invalidate(string $key, ?string $group = null): void
    {
        $this->delete($key, $group);
    }
 
    /**
     * Clear all cache entries for a group
     * @param string $group The group to clear
     * @return bool
     */
    public static function invalidateGroup(string $group): bool
    {
        $group = jvbNoBase($group);
 
        if (wp_using_ext_object_cache()) {
            // With Redis/Memcached, use native group flush
            if (function_exists('wp_cache_flush_group')) {
                return wp_cache_flush_group($group);
            } else {
                // Fallback for older WP versions - flush everything (not ideal)
                return wp_cache_flush();
            }
        } else {
            // For transients, we need to delete them from database
            global $wpdb;
 
            $prefix = self::getTransientPrefix($group);
 
            // Delete transients and their timeouts
            $sql = "DELETE FROM {$wpdb->options}
                    WHERE option_name LIKE %s
                    OR option_name LIKE %s";
 
            $result = $wpdb->query($wpdb->prepare(
                $sql,
                '_transient_' . $prefix . '%',
                '_transient_timeout_' . $prefix . '%'
            ));
 
            return $result !== false;
        }
    }
 
    /**
     * Clear cache entries by pattern (only works efficiently with Redis)
     * @param string $pattern
     * @return int
     */
    public function clearPattern(string $pattern): int
    {
        $count = 0;
 
        if (static::$use_object_cache) {
            // With Redis, this could be implemented with SCAN command
            // but wp_cache_* doesn't expose this, so we'd need direct Redis access
            // For now, just flush the group as a nuclear option
            if (function_exists('wp_cache_flush_group')) {
                wp_cache_flush_group($this->group);
                return $count;
            }
        } else {
            // For transients, search and delete
            global $wpdb;
 
            $prefix = self::getTransientPrefix($this->group);
            $sql = "SELECT option_name FROM {$wpdb->options}
                    WHERE option_name LIKE %s
                    AND option_name LIKE %s";
 
            $keys = $wpdb->get_col($wpdb->prepare(
                $sql,
                '_transient_' . $prefix . '%',
                '%' . $pattern . '%'
            ));
 
            foreach ($keys as $key) {
                $transient_key = str_replace('_transient_', '', $key);
                delete_transient($transient_key);
                $count++;
            }
        }
 
        return $count;
    }
 
    /**
     * Helper to generateKey from array if applicable
     * @param string|array $key
     * @return string
     */
    private function normalizeKey(string|array $key): string
    {
        return is_array($key) ? $this->generateKey($key) : $key;
    }
 
    /**
     * Generate a cache key from parameters
     * @param array $params An array of key/values that differentiates this cache item from others
     * @return string
     */
    public function generateKey(array $params): string
    {
        // Sort params for consistent key generation
        ksort($params);
        return md5(serialize($params));
    }
 
    /**
     * The workhorse shorthand of CacheManager. Tests the cache, and calls the callback if nothing is found.
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param callable $callback The callback to generate the value for this key
     * @param int|null $ttl The time-to-live for the cache. Defaults to constructor
     * @param string|null $group The group to save cache to. Defaults to constructor
     * @return mixed
     */
    public function remember(string|array $key, callable $callback, ?int $ttl = null, ?string $group = null): mixed
    {
        $group = $group ?: $this->group;
        $ttl = $ttl ?: $this->cache_ttl;
 
        $key = $this->normalizeKey($key);
 
        $value = $this->get($key, $group);
        if ($value === false) {
            $value = $callback();
            if ($value !== false) {
                $value = [
                    'data' => $value,
                    'last_modified' => time(),
                ];
                $this->set($key, $value, $ttl, $group);
            }
        }
 
        return (is_array($value) && array_key_exists('data', $value)) ? $value['data']: $value;
    }
 
    /**
     * Build the cache key
     * @param string $key
     * @return string
     */
    private function buildKey(string $key): string
    {
        return $this->prefix . $key;
    }
 
    /**
     * Get transient key for fallback mode
     * @param string $key
     * @param string $group
     * @return string
     */
    private function getTransientKey(string $key, string $group): string
    {
        // Transients have a 172 character limit
        $full_key = $group . '_' . $key;
 
        if (strlen($full_key) > 160) {
            // Use hash for long keys, but keep group prefix for clearPattern()
            return substr($group, 0, 20) . '_' . md5($full_key);
        }
 
        return $full_key;
    }
 
    /**
     * Get transient prefix for a group
     */
    private static function getTransientPrefix(string $group): string
    {
        return $group . '_jvb_';
    }
 
    /**
     * Check if using object cache
     */
    public function isUsingObjectCache(): bool
    {
        return static::$use_object_cache;
    }
 
 
    /**
     * Cleanup expired transients (maintenance method for non-Redis environments)
     */
    public static function cleanupExpiredTransients(): int
    {
        if (wp_using_ext_object_cache()) {
            return 0; // Not needed with Redis
        }
 
        global $wpdb;
 
        // Delete expired transients
        $sql = "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
                WHERE a.option_name LIKE '_transient_%'
                AND a.option_name NOT LIKE '_transient_timeout_%'
                AND b.option_name = CONCAT('_transient_timeout_', SUBSTRING(a.option_name, 12))
                AND b.option_value < %d";
 
        return $wpdb->query($wpdb->prepare($sql, time()));
    }
}