-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
38 lines (32 loc) · 805 Bytes
/
Cache.php
File metadata and controls
38 lines (32 loc) · 805 Bytes
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
<?php
abstract class Cache {
var $dir;
public function __construct($dir) {
$this->dir = $dir;
if (!file_exists($dir)) {
mkdir($dir);
}
}
public function get($id) {
// XXX this should include an incremental cleanup mechanism
$hash = md5($id);
$d1 = $this->dir."/".substr($hash,0,2);
$d2 = $d1."/".substr($hash,2,2);
$file = $d2."/".$hash;
if (file_exists($file)) {
//print get_class($this).": returning hit on $id\n";
return file_get_contents($file);
}
$data = $this->acquire($id);
if ($data===FALSE) return FALSE;
if (!file_exists($d1)) {
mkdir($d1);
}
if (!file_exists($d2)) {
mkdir($d2);
}
file_put_contents($file, $data);
return $data;
}
abstract protected function acquire($id);
}