53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace sacy;
|
|
|
|
class FileCache
|
|
{
|
|
private $cache_dir;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->cache_dir = implode(DIRECTORY_SEPARATOR, [
|
|
ASSET_COMPILE_OUTPUT_DIR,
|
|
'fragments',
|
|
]);
|
|
if (!is_dir($this->cache_dir)) {
|
|
if (!@mkdir($this->cache_dir, 0755, true)) {
|
|
throw new Exception('Failed to create fragments cache directory');
|
|
}
|
|
}
|
|
}
|
|
|
|
public function key2file($key)
|
|
{
|
|
if (!preg_match('#^[0-9a-z]+$#', $key)) {
|
|
throw new Exception('Invalid cache key');
|
|
}
|
|
|
|
return implode(DIRECTORY_SEPARATOR, [
|
|
$this->cache_dir,
|
|
preg_replace('#^([0-9a-f]{2})([0-9a-f]{2})(.*)$#u', '\1/\2/\3', $key),
|
|
]);
|
|
}
|
|
|
|
public function get($key)
|
|
{
|
|
return null;
|
|
$p = $this->key2file($key);
|
|
|
|
return file_exists($p) ? @file_get_contents($p) : null;
|
|
}
|
|
|
|
public function set($key, $value)
|
|
{
|
|
return true;
|
|
$p = $this->key2file($key);
|
|
if (!@mkdir(dirname($p), 0755, true)) {
|
|
throw new Exception("Failed to create fragment cache dir: {$p}");
|
|
}
|
|
|
|
return @file_put_contents($p, $value);
|
|
}
|
|
}
|