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 ); } }