| | |
| | | $key = $this->normalizeKey($key); |
| | | $cache_key = $this->buildKey($key); |
| | | |
| | | return wp_cache_get($cache_key, $group); |
| | | $value = wp_cache_get($cache_key, $group); |
| | | |
| | | // Fallback to transient if no external object cache |
| | | if ($value === false && !wp_using_ext_object_cache()) { |
| | | $value = get_transient($group . '_' . $cache_key); |
| | | } |
| | | |
| | | return $value; |
| | | } |
| | | |
| | | /** |
| | |
| | | $key = $this->normalizeKey($key); |
| | | $cache_key = $this->buildKey($key); |
| | | |
| | | // Update timestamp when setting new data |
| | | self::updateTimestamp($this->group); |
| | | |
| | | return wp_cache_set($cache_key, $value, $group, $ttl); |
| | | } |
| | | // Try object cache first |
| | | $result = wp_cache_set($cache_key, $value, $group, $ttl); |
| | | |
| | | // If no external object cache, also store in transient for persistence |
| | | if (!wp_using_ext_object_cache()) { |
| | | set_transient($group . '_' . $cache_key, $value, $ttl); |
| | | } |
| | | |
| | | return $result; |
| | | } |
| | | /** |
| | | * Delete a cached value |
| | | * @param string|array $key The key to look up (auto-generates key from array of key=>values) |
| | |
| | | $key = $this->normalizeKey($key); |
| | | $cache_key = $this->buildKey($key); |
| | | |
| | | return wp_cache_delete($cache_key, $group); |
| | | $result = wp_cache_delete($cache_key, $group); |
| | | |
| | | // Also delete transient if no external object cache |
| | | if (!wp_using_ext_object_cache()) { |
| | | delete_transient($group . '_' . $cache_key); |
| | | } |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Clear all cache for this group |
| | | * @return bool |
| | |
| | | try { |
| | | if (function_exists('wp_cache_flush_group')) { |
| | | wp_cache_flush_group($this->group); |
| | | self::updateTimestamp($this->group); |
| | | return true; |
| | | } |
| | | return false; |
| | | |
| | | // Clear transients for this group if no external object cache |
| | | if (!wp_using_ext_object_cache()) { |
| | | $this->clearGroupTransients(); |
| | | } |
| | | |
| | | self::updateTimestamp($this->group); |
| | | return true; |
| | | } catch (\Exception $e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Clear all transients for this cache group |
| | | */ |
| | | private function clearGroupTransients(): void |
| | | { |
| | | global $wpdb; |
| | | |
| | | $pattern = '_transient_' . $this->group . '_' . $this->prefix . '%'; |
| | | $timeout_pattern = '_transient_timeout_' . $this->group . '_' . $this->prefix . '%'; |
| | | |
| | | $wpdb->query( |
| | | $wpdb->prepare( |
| | | "DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s", |
| | | $pattern, |
| | | $timeout_pattern |
| | | ) |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Helper to generateKey from array if applicable |
| | | * @param string|array $key |
| | | * @return string |