/
fragment-cache.php
39 lines (34 loc) · 1.12 KB
/
fragment-cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace sacy;
class FileCache{
private $cache_dir;
function __construct(){
$this->cache_dir = implode(DIRECTORY_SEPARATOR, array(
ASSET_COMPILE_OUTPUT_DIR,
'cache'
));
if (!is_dir($this->cache_dir)){
if (!@mkdir($this->cache_dir, 0755, true)){
throw new Exception("Failed to create fragments cache directory");
}
}
}
function key2file($key){
if (!preg_match('#^[0-9a-z]+$#', $key)) throw new Exception('Invalid cache key');
return implode(DIRECTORY_SEPARATOR, array(
$this->cache_dir,
preg_replace('#^([0-9a-f]{2})([0-9a-f]{2})(.*)$#u', '\1/\2/\3', $key)
));
}
function get($key){
$p = $this->key2file($key);
return file_exists($p) ? @unserialize(file_get_contents($p)) : null;
}
function set($key, $value){
$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, serialize($value));
}
}