tigerstyle-dash/includes/class-cache.php
Ryan Malloy 31ce71dac0 Initial commit: TigerStyle Dash v1.0.0
WordPress Performance at Lightning Speed.

Advanced optimization with Brotli compression, smart caching, and
cat-like speed bursts that dash past the competition.

- Brotli and Gzip compression with configurable levels
- WP Rocket-inspired smart caching
- Bandwidth monitoring
- Automatic optimization
- Performance status dashboard

Includes build.sh and .distignore for WordPress-installable release ZIPs.
2026-05-27 14:32:00 -06:00

134 lines
3.4 KiB
PHP

<?php
/**
* Cache Manager for TigerStyle Dash
* Lightning-fast cache operations
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
class TigerStyle_Dash_Cache {
/**
* Cache directory
*/
private $cache_dir;
/**
* Constructor
*/
public function __construct() {
$upload_dir = wp_upload_dir();
$this->cache_dir = $upload_dir['basedir'] . '/tigerstyle-dash-cache/';
// Ensure cache directory exists
if (!file_exists($this->cache_dir)) {
wp_mkdir_p($this->cache_dir);
}
}
/**
* Get cache key for URL
*/
public function get_cache_key($url) {
return md5($url);
}
/**
* Store compressed content in cache
*/
public function store($key, $content, $compression_method, $metadata = array()) {
$cache_file = $this->cache_dir . $key . '.' . $compression_method;
$meta_file = $this->cache_dir . $key . '.meta';
// Store compressed content
file_put_contents($cache_file, $content);
// Store metadata
$metadata['created'] = time();
$metadata['compression_method'] = $compression_method;
$metadata['size'] = strlen($content);
file_put_contents($meta_file, json_encode($metadata));
return true;
}
/**
* Retrieve cached content
*/
public function get($key, $compression_method) {
$cache_file = $this->cache_dir . $key . '.' . $compression_method;
$meta_file = $this->cache_dir . $key . '.meta';
if (!file_exists($cache_file) || !file_exists($meta_file)) {
return false;
}
$metadata = json_decode(file_get_contents($meta_file), true);
if (!$metadata) {
return false;
}
// Check if cache is expired
$ttl = get_option('tigerstyle_dash_cache_ttl', 3600);
if (time() - $metadata['created'] > $ttl) {
$this->delete($key);
return false;
}
return array(
'content' => file_get_contents($cache_file),
'metadata' => $metadata
);
}
/**
* Delete cached content
*/
public function delete($key) {
$files = glob($this->cache_dir . $key . '.*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
return true;
}
/**
* Clear all cache
*/
public function clear_all_cache() {
$files = glob($this->cache_dir . '*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
return true;
}
/**
* Get cache statistics
*/
public function get_cache_stats() {
$files = glob($this->cache_dir . '*.meta');
$total_files = count($files);
$total_size = 0;
foreach ($files as $file) {
$metadata = json_decode(file_get_contents($file), true);
if ($metadata && isset($metadata['size'])) {
$total_size += $metadata['size'];
}
}
return array(
'total_files' => $total_files,
'total_size' => $total_size,
'cache_dir' => $this->cache_dir
);
}
}