refactor(Core): refactor and optimize code
This commit is contained in:
137
app/Core/Cache.php
Normal file
137
app/Core/Cache.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
namespace Sodino\Core;
|
||||
|
||||
/**
|
||||
* Cache Manager
|
||||
* Handles caching with WordPress transients and custom database cache
|
||||
*/
|
||||
class Cache {
|
||||
private static $instance = null;
|
||||
private $memory_cache = [];
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached value
|
||||
*/
|
||||
public function get($key, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Check memory cache first
|
||||
if (isset($this->memory_cache[$full_key])) {
|
||||
return $this->memory_cache[$full_key];
|
||||
}
|
||||
|
||||
// Check WordPress transient
|
||||
$value = get_transient($full_key);
|
||||
|
||||
if ($value !== false) {
|
||||
$this->memory_cache[$full_key] = $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached value
|
||||
*/
|
||||
public function set($key, $value, $expiration = 3600, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Set in memory cache
|
||||
$this->memory_cache[$full_key] = $value;
|
||||
|
||||
// Set in WordPress transient
|
||||
return set_transient($full_key, $value, $expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cached value
|
||||
*/
|
||||
public function delete($key, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Remove from memory cache
|
||||
unset($this->memory_cache[$full_key]);
|
||||
|
||||
// Remove from WordPress transient
|
||||
return delete_transient($full_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cache for a group
|
||||
*/
|
||||
public function clearGroup($group = 'sodino') {
|
||||
global $wpdb;
|
||||
|
||||
// Clear memory cache for group
|
||||
foreach ($this->memory_cache as $key => $value) {
|
||||
if (strpos($key, "sodino_{$group}_") === 0) {
|
||||
unset($this->memory_cache[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear transients for group
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
|
||||
$wpdb->esc_like('_transient_sodino_' . $group . '_') . '%'
|
||||
)
|
||||
);
|
||||
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
|
||||
$wpdb->esc_like('_transient_timeout_sodino_' . $group . '_') . '%'
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all Sodino cache
|
||||
*/
|
||||
public function clearAll() {
|
||||
global $wpdb;
|
||||
|
||||
// Clear memory cache
|
||||
$this->memory_cache = [];
|
||||
|
||||
// Clear all Sodino transients
|
||||
$wpdb->query(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_sodino_%' OR option_name LIKE '_transient_timeout_sodino_%'"
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remember pattern - get from cache or execute callback
|
||||
*/
|
||||
public function remember($key, $callback, $expiration = 3600, $group = 'sodino') {
|
||||
$value = $this->get($key, $group);
|
||||
|
||||
if ($value !== false) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value = call_user_func($callback);
|
||||
$this->set($key, $value, $expiration, $group);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build cache key
|
||||
*/
|
||||
private function buildKey($key, $group) {
|
||||
return "sodino_{$group}_{$key}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user