← All changes
|
vendor/wpfluent/framework/src/WPFluent/Cache/Cache.php
+37
-18
1.1.0
→
2.10.0
View file →
| @@ -57,9 +57,9 @@ | ||
| 57 | 57 | { |
| 58 | 58 | $key = static::key($key); |
| 59 | 59 | |
| 60 | 60 | if (static::isPersistent()) { |
| 61 | - return wp_cache_set($key, $value, null, $ttl); | |
| 61 | + return wp_cache_set($key, $value, static::cacheGroup(), $ttl); | |
| 62 | 62 | } else { |
| 63 | 63 | return static::filePut($key, $value, $ttl); |
| 64 | 64 | } |
| 65 | 65 | } |
| @@ -80,17 +80,20 @@ | ||
| 80 | 80 | /** |
| 81 | 81 | * Retrieve data from the cache. |
| 82 | 82 | * |
| 83 | 83 | * @param string $key |
| 84 | - * @param mixed $default | |
| 85 | 84 | * @return mixed |
| 86 | 85 | */ |
| 87 | 86 | public static function get($key) |
| 88 | 87 | { |
| 88 | + // Initialize so wp_cache_get's by-reference $found arg doesn't | |
| 89 | + // trigger a PHP 8.1+ "writing to undefined variable" deprecation. | |
| 90 | + $found = null; | |
| 91 | + | |
| 89 | 92 | $key = static::key($key); |
| 90 | 93 | |
| 91 | 94 | if (static::isPersistent()) { |
| 92 | - $value = wp_cache_get($key, null, false, $found); | |
| 95 | + $value = wp_cache_get($key, static::cacheGroup(), false, $found); | |
| 93 | 96 | $value = $found ? $value : null; |
| 94 | 97 | } else { |
| 95 | 98 | $value = static::fileGet($key) ?: null; |
| 96 | 99 | } |
| @@ -108,9 +111,9 @@ | ||
| 108 | 111 | { |
| 109 | 112 | $key = static::key($key); |
| 110 | 113 | |
| 111 | 114 | if (static::isPersistent()) { |
| 112 | - return wp_cache_delete($key, static::$cacheGroup); | |
| 115 | + return wp_cache_delete($key, static::cacheGroup()); | |
| 113 | 116 | } |
| 114 | 117 | |
| 115 | 118 | return static::fileForget($key); |
| 116 | 119 | } |
| @@ -115,8 +118,20 @@ | ||
| 115 | 118 | return static::fileForget($key); |
| 116 | 119 | } |
| 117 | 120 | |
| 118 | 121 | /** |
| 122 | + * The group key for the cache. | |
| 123 | + * | |
| 124 | + * @return string | |
| 125 | + */ | |
| 126 | + public static function cacheGroup() | |
| 127 | + { | |
| 128 | + $slug = App::config()->get('app.slug'); | |
| 129 | + | |
| 130 | + return 'wpfluent_cache_group_' . $slug; | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 119 | 134 | * Store data in the cache "forever" using 5 years of expiry time. |
| 120 | 135 | * |
| 121 | 136 | * @param string $key |
| 122 | 137 | * @param mixed $value |
| @@ -126,9 +141,9 @@ | ||
| 126 | 141 | { |
| 127 | 142 | $key = static::key($key); |
| 128 | 143 | |
| 129 | 144 | if (static::isPersistent()) { |
| 130 | - return wp_cache_set($key, $value, null, 0); | |
| 145 | + return wp_cache_set($key, $value, static::cacheGroup(), 0); | |
| 131 | 146 | } else { |
| 132 | 147 | return static::filePut($key, $value, 0); |
| 133 | 148 | } |
| 134 | 149 | } |
| @@ -151,14 +166,13 @@ | ||
| 151 | 166 | return $value; |
| 152 | 167 | } |
| 153 | 168 | |
| 154 | 169 | try { |
| 155 | - if (($value = $callback()) && !is_wp_error($value)) { | |
| 170 | + $value = $callback(); | |
| 171 | + if (!is_wp_error($value)) { | |
| 156 | 172 | static::set($key, $value, $ttl); |
| 157 | - | |
| 158 | 173 | return $value; |
| 159 | 174 | } |
| 160 | - | |
| 161 | 175 | } catch (Throwable $e) { |
| 162 | 176 | error_log("Cache callback error [{$key}]: " . $e->getMessage()); |
| 163 | 177 | } |
| 164 | 178 | } |
| @@ -205,25 +219,30 @@ | ||
| 205 | 219 | * @return void |
| 206 | 220 | */ |
| 207 | 221 | public static function flush() |
| 208 | 222 | { |
| 209 | - $key = App::config()->get('app.slug') . '_cache_'; | |
| 223 | + if (static::isPersistent()) { | |
| 224 | + // Preferred: Use wp_cache_flush_group if available | |
| 225 | + if (function_exists('wp_cache_flush_group')) { | |
| 226 | + return wp_cache_flush_group(static::cacheGroup()); | |
| 227 | + } | |
| 210 | 228 | |
| 211 | - if (static::isPersistent()) { | |
| 212 | - | |
| 229 | + // Fallback: Manual flush for in-memory cache | |
| 213 | 230 | global $wp_object_cache; |
| 214 | - | |
| 215 | - $pattern = '~^'. preg_quote($key, '~') . '~i'; | |
| 216 | - | |
| 217 | - $keys = preg_grep($pattern, array_keys($wp_object_cache->cache)); | |
| 231 | + $group = static::cacheGroup(); | |
| 218 | 232 | |
| 219 | - foreach ($keys as $key) { | |
| 220 | - unset($wp_object_cache->cache[$key]); | |
| 233 | + if (!isset($wp_object_cache->cache[$group])) { | |
| 234 | + return false; | |
| 221 | 235 | } |
| 222 | 236 | |
| 237 | + foreach (array_keys($wp_object_cache->cache[$group]) as $key) { | |
| 238 | + unset($wp_object_cache->cache[$group][$key]); | |
| 239 | + } | |
| 240 | + | |
| 223 | 241 | return true; |
| 224 | 242 | } |
| 225 | 243 | |
| 244 | + // For non-persistent cache (e.g., file cache fallback) | |
| 226 | 245 | return static::fileFlush(); |
| 227 | 246 | } |
| 228 | 247 | |
| 229 | 248 | /** |
| @@ -246,9 +265,9 @@ | ||
| 246 | 265 | protected static function key($key) |
| 247 | 266 | { |
| 248 | 267 | $slug = App::config()->get('app.slug'); |
| 249 | 268 | |
| 250 | - return $slug . '_cache_' . sanitize_key($key); | |
| 269 | + return $slug . '_wpfluent_cache_' . sanitize_key($key); | |
| 251 | 270 | } |
| 252 | 271 | |
| 253 | 272 | /** |
| 254 | 273 | * Check if persistent cache is available. |