-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
cachefiles.php
executable file
·100 lines (67 loc) · 2.08 KB
/
cachefiles.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
class cmsCacheFiles {
private $cache_path;
public function __construct($config) {
$this->cache_path = $config->cache_path . 'data/';
}
public function set($key, $value, $ttl) {
$data = [
'ttl' => $ttl,
'time' => time(),
'value' => serialize($value)
];
list($path, $file_path) = $this->getPathAndFile($key);
@mkdir($path, 0777, true);
@chmod($path, 0777);
@chmod(pathinfo($path, PATHINFO_DIRNAME), 0777);
$file_path_tmp = $file_path . '.tmp';
$success = file_put_contents($file_path_tmp, '<?php return ' . var_export($data, true) . ';');
if ($success) {
rename($file_path_tmp, $file_path);
}
return $success;
}
public function has($key) {
list($path, $file) = $this->getPathAndFile($key);
return is_readable($file);
}
public function get($key) {
list($path, $file) = $this->getPathAndFile($key);
$data = include $file;
if (!$data) {
return false;
}
if (!isset($data['value']) ||
!isset($data['time']) ||
!isset($data['ttl']) ||
time() > ($data['time'] + $data['ttl'])) {
$this->clean($key);
return false;
}
return unserialize($data['value']);
}
public function clean($key = false) {
if ($key) {
$path = $this->cache_path . str_replace('.', '/', $key);
if (is_file($path . '.dat')) {
@unlink($path . '.dat');
}
return files_remove_directory($path);
} else {
return files_clear_directory($this->cache_path);
}
}
public function getPathAndFile($key) {
$path = $this->cache_path . str_replace('.', '/', $key);
return [dirname($path), $path . '.dat'];
}
public function start() {
return true;
}
public function stop() {
return true;
}
public function getStats() {
return [];
}
}