← All changes
|
vendor/wpfluent/framework/src/WPFluent/Cache/Cache.php
+33
-18
1.41
→
2.1.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,9 +80,8 @@ | ||
| 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,9 +87,9 @@ | ||
| 88 | 87 | { |
| 89 | 88 | $key = static::key($key); |
| 90 | 89 | |
| 91 | 90 | if (static::isPersistent()) { |
| 92 | - $value = wp_cache_get($key, null, false, $found); | |
| 91 | + $value = wp_cache_get($key, static::cacheGroup(), false, $found); | |
| 93 | 92 | $value = $found ? $value : null; |
| 94 | 93 | } else { |
| 95 | 94 | $value = static::fileGet($key) ?: null; |
| 96 | 95 | } |
| @@ -108,9 +107,9 @@ | ||
| 108 | 107 | { |
| 109 | 108 | $key = static::key($key); |
| 110 | 109 | |
| 111 | 110 | if (static::isPersistent()) { |
| 112 | - return wp_cache_delete($key, static::$cacheGroup); | |
| 111 | + return wp_cache_delete($key, static::cacheGroup()); | |
| 113 | 112 | } |
| 114 | 113 | |
| 115 | 114 | return static::fileForget($key); |
| 116 | 115 | } |
| @@ -115,8 +114,20 @@ | ||
| 115 | 114 | return static::fileForget($key); |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | 117 | /** |
| 118 | + * The group key for the cache. | |
| 119 | + * | |
| 120 | + * @return string | |
| 121 | + */ | |
| 122 | + public static function cacheGroup() | |
| 123 | + { | |
| 124 | + $slug = App::config()->get('app.slug'); | |
| 125 | + | |
| 126 | + return 'wpfluent_cache_group_' . $slug; | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 119 | 130 | * Store data in the cache "forever" using 5 years of expiry time. |
| 120 | 131 | * |
| 121 | 132 | * @param string $key |
| 122 | 133 | * @param mixed $value |
| @@ -126,9 +137,9 @@ | ||
| 126 | 137 | { |
| 127 | 138 | $key = static::key($key); |
| 128 | 139 | |
| 129 | 140 | if (static::isPersistent()) { |
| 130 | - return wp_cache_set($key, $value, null, 0); | |
| 141 | + return wp_cache_set($key, $value, static::cacheGroup(), 0); | |
| 131 | 142 | } else { |
| 132 | 143 | return static::filePut($key, $value, 0); |
| 133 | 144 | } |
| 134 | 145 | } |
| @@ -151,14 +162,13 @@ | ||
| 151 | 162 | return $value; |
| 152 | 163 | } |
| 153 | 164 | |
| 154 | 165 | try { |
| 155 | - if (($value = $callback()) && !is_wp_error($value)) { | |
| 166 | + $value = $callback(); | |
| 167 | + if (!is_wp_error($value)) { | |
| 156 | 168 | static::set($key, $value, $ttl); |
| 157 | - | |
| 158 | 169 | return $value; |
| 159 | 170 | } |
| 160 | - | |
| 161 | 171 | } catch (Throwable $e) { |
| 162 | 172 | error_log("Cache callback error [{$key}]: " . $e->getMessage()); |
| 163 | 173 | } |
| 164 | 174 | } |
| @@ -205,25 +215,30 @@ | ||
| 205 | 215 | * @return void |
| 206 | 216 | */ |
| 207 | 217 | public static function flush() |
| 208 | 218 | { |
| 209 | - $key = App::config()->get('app.slug') . '_cache_'; | |
| 219 | + if (static::isPersistent()) { | |
| 220 | + // Preferred: Use wp_cache_flush_group if available | |
| 221 | + if (function_exists('wp_cache_flush_group')) { | |
| 222 | + return wp_cache_flush_group(static::cacheGroup()); | |
| 223 | + } | |
| 210 | 224 | |
| 211 | - if (static::isPersistent()) { | |
| 212 | - | |
| 225 | + // Fallback: Manual flush for in-memory cache | |
| 213 | 226 | global $wp_object_cache; |
| 214 | - | |
| 215 | - $pattern = '~^'. preg_quote($key, '~') . '~i'; | |
| 216 | - | |
| 217 | - $keys = preg_grep($pattern, array_keys($wp_object_cache->cache)); | |
| 227 | + $group = static::cacheGroup(); | |
| 218 | 228 | |
| 219 | - foreach ($keys as $key) { | |
| 220 | - unset($wp_object_cache->cache[$key]); | |
| 229 | + if (!isset($wp_object_cache->cache[$group])) { | |
| 230 | + return false; | |
| 221 | 231 | } |
| 222 | 232 | |
| 233 | + foreach (array_keys($wp_object_cache->cache[$group]) as $key) { | |
| 234 | + unset($wp_object_cache->cache[$group][$key]); | |
| 235 | + } | |
| 236 | + | |
| 223 | 237 | return true; |
| 224 | 238 | } |
| 225 | 239 | |
| 240 | + // For non-persistent cache (e.g., file cache fallback) | |
| 226 | 241 | return static::fileFlush(); |
| 227 | 242 | } |
| 228 | 243 | |
| 229 | 244 | /** |
| @@ -246,9 +261,9 @@ | ||
| 246 | 261 | protected static function key($key) |
| 247 | 262 | { |
| 248 | 263 | $slug = App::config()->get('app.slug'); |
| 249 | 264 | |
| 250 | - return $slug . '_cache_' . sanitize_key($key); | |
| 265 | + return $slug . '_wpfluent_cache_' . sanitize_key($key); | |
| 251 | 266 | } |
| 252 | 267 | |
| 253 | 268 | /** |
| 254 | 269 | * Check if persistent cache is available. |