GIF94;
| Path : /var/www/wordpress/wp-content/plugins/oxymade/includes/ |
| Current File : /var/www/wordpress/wp-content/plugins/oxymade/includes/class-oxymade-cache.php |
<?php
/**
* OxyMade Cache Class
*
* Handles caching for options and transients to reduce database queries
*
* @package OxyMade
* @since 0.1.6
*/
namespace OxyMade\Includes;
class Cache {
/**
* In-memory option cache
*
* @var array
*/
private static $option_cache = array();
/**
* In-memory transient cache
*
* @var array
*/
private static $transient_cache = array();
/**
* Cache hit counter for statistics
*
* @var int
*/
private static $hits = 0;
/**
* Cache miss counter for statistics
*
* @var int
*/
private static $misses = 0;
/**
* Get option with caching
*
* @param string $key Option key
* @param mixed $default Default value if option doesn't exist
* @return mixed Option value
*/
public static function get_option($key, $default = null) {
// Check memory cache first
if (isset(self::$option_cache[$key])) {
self::$hits++;
return self::$option_cache[$key];
}
self::$misses++;
// Get from database
$value = \get_option($key, $default);
// Cache in memory
self::$option_cache[$key] = $value;
return $value;
}
/**
* Update option and cache
*
* @param string $key Option key
* @param mixed $value Option value
* @param string|bool $autoload Whether to autoload option
* @return bool True on success
*/
public static function update_option($key, $value, $autoload = null) {
$result = \update_option($key, $value, $autoload);
if ($result) {
self::$option_cache[$key] = $value;
}
return $result;
}
/**
* Delete option and cache
*
* @param string $key Option key
* @return bool True on success
*/
public static function delete_option($key) {
$result = \delete_option($key);
if ($result) {
unset(self::$option_cache[$key]);
}
return $result;
}
/**
* Get transient with caching
*
* @param string $key Transient key
* @return mixed Transient value or false
*/
public static function get_transient($key) {
// Check memory cache first
if (isset(self::$transient_cache[$key])) {
self::$hits++;
return self::$transient_cache[$key];
}
self::$misses++;
// Get from database
$value = \get_transient($key);
// Cache in memory
if (false !== $value) {
self::$transient_cache[$key] = $value;
}
return $value;
}
/**
* Set transient and cache
*
* @param string $key Transient key
* @param mixed $value Transient value
* @param int $expiration Expiration time in seconds
* @return bool True on success
*/
public static function set_transient($key, $value, $expiration = 0) {
$result = \set_transient($key, $value, $expiration);
if ($result) {
self::$transient_cache[$key] = $value;
}
return $result;
}
/**
* Delete transient and cache
*
* @param string $key Transient key
* @return bool True on success
*/
public static function delete_transient($key) {
$result = \delete_transient($key);
if ($result) {
unset(self::$transient_cache[$key]);
}
return $result;
}
/**
* Clear all caches
*
* @return void
*/
public static function clear_all() {
self::$option_cache = array();
self::$transient_cache = array();
// Clear WordPress object cache for oxymade keys only
if (function_exists('wp_cache_delete_multiple')) {
\wp_cache_delete_multiple(
array_keys(self::$option_cache),
'options'
);
}
}
/**
* Clear specific cache group
*
* @param string $group Group prefix to clear
* @return void
*/
public static function clear_group($group) {
$keys_to_clear = array();
foreach (self::$option_cache as $key => $value) {
if (strpos($key, $group) === 0) {
$keys_to_clear[] = $key;
}
}
foreach ($keys_to_clear as $key) {
unset(self::$option_cache[$key]);
\wp_cache_delete($key, 'options');
}
// Clear transients with group prefix
$transient_keys = array();
foreach (self::$transient_cache as $key => $value) {
if (strpos($key, $group) === 0) {
$transient_keys[] = $key;
}
}
foreach ($transient_keys as $key) {
unset(self::$transient_cache[$key]);
\delete_transient($key);
}
}
/**
* Get cache statistics
*
* @return array Cache statistics
*/
public static function get_stats() {
return array(
'option_cache_count' => count(self::$option_cache),
'transient_cache_count' => count(self::$transient_cache),
'option_cache_size' => strlen(serialize(self::$option_cache)),
'transient_cache_size' => strlen(serialize(self::$transient_cache)),
'hits' => self::$hits,
'misses' => self::$misses,
'hit_rate' => self::$hits + self::$misses > 0
? round((self::$hits / (self::$hits + self::$misses)) * 100, 2)
: 0
);
}
/**
* Warm up cache with commonly used options
*
* @return void
*/
public static function warm_up() {
$common_options = array(
'oxymade_settings',
'oxymade_color_palette',
'oxymade_color_palette_flat',
'oxymade_variables_synced',
'oxymade_default_designset_template'
);
foreach ($common_options as $option) {
self::get_option($option);
}
}
/**
* Reset cache statistics
*
* @return void
*/
public static function reset_stats() {
self::$hits = 0;
self::$misses = 0;
}
}