77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
interface CacheInterface {
|
|
public function get(string $key);
|
|
public function set(string $key, $value, int $ttl = 3600): bool;
|
|
public function delete(string $key): bool;
|
|
public function clear(): bool;
|
|
public function has(string $key): bool;
|
|
}
|
|
|
|
class FileCache implements CacheInterface {
|
|
private string $cacheDir;
|
|
|
|
public function __construct(string $cacheDir = '/tmp/codepress_cache') {
|
|
$this->cacheDir = $cacheDir;
|
|
if (!is_dir($this->cacheDir)) {
|
|
mkdir($this->cacheDir, 0755, true);
|
|
}
|
|
}
|
|
|
|
public function get(string $key) {
|
|
$file = $this->getCacheFile($key);
|
|
if (!file_exists($file)) {
|
|
return null;
|
|
}
|
|
|
|
$data = unserialize(file_get_contents($file));
|
|
if ($data['expires'] < time()) {
|
|
unlink($file);
|
|
return null;
|
|
}
|
|
|
|
return $data['value'];
|
|
}
|
|
|
|
public function set(string $key, $value, int $ttl = 3600): bool {
|
|
$file = $this->getCacheFile($key);
|
|
$data = [
|
|
'value' => $value,
|
|
'expires' => time() + $ttl
|
|
];
|
|
|
|
return file_put_contents($file, serialize($data)) !== false;
|
|
}
|
|
|
|
public function delete(string $key): bool {
|
|
$file = $this->getCacheFile($key);
|
|
if (file_exists($file)) {
|
|
return unlink($file);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function clear(): bool {
|
|
$files = glob($this->cacheDir . '/*');
|
|
foreach ($files as $file) {
|
|
if (is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function has(string $key): bool {
|
|
$file = $this->getCacheFile($key);
|
|
if (!file_exists($file)) {
|
|
return false;
|
|
}
|
|
|
|
$data = unserialize(file_get_contents($file));
|
|
return $data['expires'] > time();
|
|
}
|
|
|
|
private function getCacheFile(string $key): string {
|
|
return $this->cacheDir . '/' . md5($key) . '.cache';
|
|
}
|
|
} |