<?php
// Simple file-based cache system for ultra-fast responses
class APICache {
    private $cacheDir = __DIR__ . '/../.cache';

    public function __construct() {
        if (!is_dir($this->cacheDir)) {
            mkdir($this->cacheDir, 0755, true);
        }
    }

    public function get($key) {
        $file = $this->cacheDir . '/' . md5($key);
        if (file_exists($file)) {
            $data = json_decode(file_get_contents($file), true);
            if ($data['expires'] > time()) {
                return $data['value'];
            }
            unlink($file);
        }
        return null;
    }

    public function set($key, $value, $ttl = 300) {
        $file = $this->cacheDir . '/' . md5($key);
        $data = [
            'value' => $value,
            'expires' => time() + $ttl
        ];
        file_put_contents($file, json_encode($data));
    }

    public function flush($pattern = null) {
        if ($pattern) {
            foreach (glob($this->cacheDir . '/*') as $file) {
                if (strpos(basename($file), md5($pattern)) !== false || strpos($pattern, '*') !== false) {
                    @unlink($file);
                }
            }
        } else {
            array_map('unlink', glob($this->cacheDir . '/*'));
        }
    }
}

$cache = new APICache();
?>
