powercache.php
2.49 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
<?php
class Powercache {
private static $instance;
private $file;
public static function getInstance()
{
if ( is_null( self::$instance ) )
{
self::$instance = new self();
}
return self::$instance;
}
public static function get($file, $key = null) {
$c = self::getInstance();
if (!isset($c->file[$file])) {
if (!file_exists(DIR_CACHE . 'pcache.' . $file))
return null;
$c->file[$file] = include DIR_CACHE . 'pcache.' . $file;
}
if (!$key)
return $c->file[$file];
if (!is_array($c->file[$file]) || !array_key_exists($key, $c->file[$file]))
return null;
return $c->file[$file][$key];
}
public static function set($file, $value) {
file_put_contents(DIR_CACHE . 'pcache.' . $file, "<?php\nreturn " . var_export($value, true) . ";\n", LOCK_EX);
}
public static function add($file, $key, $value) {
if (file_exists(DIR_CACHE . 'pcache.' . $file)) {
$arr = include DIR_CACHE . 'pcache.' . $file;
} else {
$arr = array();
}
if (!is_array($arr)) {
$arr = array();
}
$arr[$key] = $value;
file_put_contents(DIR_CACHE . 'pcache.' . $file, "<?php\nreturn " . var_export($arr, true) . ";\n", LOCK_EX);
}
public static function remove($file, $key, $val = null) {
$files = array();
if (file_exists(DIR_CACHE . 'pcache.' . $file)) {
$files[] = DIR_CACHE . 'pcache.' . $file;
} else {
$glob = glob(DIR_CACHE . 'pcache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $file) . '.*');
if ($glob) {
foreach ($glob as $file) {
if (file_exists($file)) {
$files[] = $file;
}
}
}
}
foreach ($files as $file) {
$arr = include $file;
$changed = false;
if (!is_null($val)) {
foreach(array_keys($arr) as $node) {
if (strpos($node, $key) !== false) {
$values = explode('_', str_replace($key, '', $node));
if (in_array($val, $values)) {
$changed = true;
unset($arr[$node]);
}
}
}
} else {
if (isset($arr[$key])) {
$changed = true;
unset($arr[$key]);
}
}
if ($changed) {
file_put_contents($file, "<?php\nreturn " . var_export($arr, true) . ";\n", LOCK_EX);
}
}
}
public static function delete($file) {
if (file_exists(DIR_CACHE . 'pcache.' . $file)) {
return unlink(DIR_CACHE . 'pcache.' . $file);
}
$files = glob(DIR_CACHE . 'pcache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $file) . '.*');
if ($files) {
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
}
}
?>