-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.php
202 lines (195 loc) · 5.67 KB
/
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace c;
/**
* Cache compress class
* @author Kosmom <Kosmom.ru>
*/
class cache{
private static $cacheTimeout=0;
private static $cachePath='cache';
private static $replaces=array();
static function refresh_page($link){
$cachefile=self::getPath().'/'.\md5($link).'.gz';
if (\file_exists($cachefile)) \unlink($cachefile);
if (isset($_SESSION)){
$temp=$_SESSION;
unset($_SESSION);
}
$text=\file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/'.$link);
if (!$text)return \false;
\file_put_contents($cachefile,\gzencode(self::compressHtml($text),9));
if (isset($temp))$_SESSION=$temp;
return \true;
}
/**
* @deprecated since version 3.4
*/
static function compress_html($compress){
return self::compressHtml($compress);
}
static function compressHtml($compress){
self::$replaces=array();
$compress=self::decodeHtml($compress);
$compress=\preg_replace('/\s+/',' ',$compress);
if (empty(self::$replaces))return $compress;
return self::encodeHtml($compress);
}
/**
* @deprecated since version 3.4
*/
static function decode_html($text){
return self::decodeHtml($text);
}
static function decodeHtml($text){
return \preg_replace_callback('/<pre[>\s].*<\/pre>|<img.*>|<script>.*<\/script>/sU','self::decodes',$text);
}
private static function decodes($matches){
\array_push(self::$replaces,$matches[0]);
return '{{{replace}}}';
}
/**
* @deprecated since version 3.4
*/
static function encode_html($text){
return self::encodeHtml($text);
}
static function encodeHtml($text){
return \preg_replace_callback('/{{{replace}}}/','self::encodes', $text);
}
private static function encodes($matches){
return \array_shift(self::$replaces);
}
/**
* @deprecated since version 3.4
*/
static function clear_page($link){
return self::clearPage($link);
}
static function clearPage($link){
$cachefile=self::getPath().'/'.\md5($link).'.gz';
if (\file_exists($cachefile))\unlink($cachefile);
return \true;
}
static function delete($key){
return self::clear($key);
}
static function clear($key=\null){
$path=self::getPath();
if ($key===\null){
if (!$handle=\opendir($path))return \false;
while(\false !== ($file=\readdir($handle))){
if ($file == '.' or $file == '..' or (\substr($file,-6) != '.cache' && \substr($file,-3) != '.gz') or \is_dir($file)) continue;
\unlink($path.'/'.$file);
}
\closedir($handle);
return \true;
}
if (\strpos($key,'*')===\false)return \unlink($path.'/'.$key.'.cache');
foreach (\glob($path.'/'.$key.'.cache') as $file)\unlink($file);
}
static function check($files){
$compare=array();
foreach($files as $key=>$item){
if ($key=='index')$key='';
$compare[\md5($key).'.gz']=1;
}
$files=0;
if (!$handle=\opendir(self::getPath()))return \false;
while(\false !== ($file=\readdir($handle))){
if ($file == '.' or $file == '..' or substr($file,-3) != '.gz' or \is_dir($file) or !$compare[$file])return \false;
$files++;
}
\closedir($handle);
return \sizeof($compare)==$files;
}
/**
* @deprecated since version 3.4
*/
static function is_cleared(){
return self::isCleared();
}
static function isCleared(){
if (!$handle=\opendir(self::getPath()))return ;
while(\false !== ($file=\readdir($handle))){
if ($file == '.' or $file == '..' or \substr($file,-3) != '.gz' or \is_dir($file)) continue;
return \false;
}
\closedir($handle);
return \true;
}
/**
* Get or set timeout parameter
* @param null|int $timeout
* @return int
*/
static function timeout($timeout=\null){
if ($timeout===\null)return self::$cacheTimeout;
return self::$cacheTimeout=(int)$timeout;
}
/**
* Get or set path parameter
* @param null|string $path
* @return string
*/
static function path($path=\null){
if ($path===\null)return self::$cachePath;
return self::$cachePath=(string)$path;
}
private static function getPath(){
return isset(core::$data['cachePath'])?core::$data['cachePath']:self::$cachePath;
}
/**
* Get key from cache
* @param string $key
* @param mixed $callback If callback type - callback with set key. Else - default value
* @param null|int $timeout
* @return mixed
*/
static function get($key,$callback=\null,$timeout=\null) {
if ($timeout===\null)$timeout=isset(core::$data['cacheTimeout'])?core::$data['cacheTimeout']:self::$cacheTimeout;
$fullpath=self::getPath().'/'.$key.'.cache';
$mtime=@\filemtime($fullpath);
if ($mtime===\false or ($timeout>0 && \time() - $mtime > $timeout)){
if (!\is_callable($callback))return $callback;
$rs=$callback($key);
filedata::savedata($fullpath, $rs);
$mtime=\filemtime($fullpath);
\clearstatcache(\false,$fullpath);
return $rs;
}
return filedata::loaddata($fullpath);
}
static function getList($mask=''){
$path=self::getPath();
return filedata::filelist($path,'/'.$mask.'.+\.cache/',function($item){
return \basename($item,'.cache');
});
}
static function getMultiple($keys,$default=\null,$timeout=\null){
$out=array();
foreach ($keys as $key){
$out[$key]=self::get($key,$default,$timeout);
}
return $out;
}
static function deleteMultiple($keys){
foreach ($keys as $key){
self::clear($key);
}
}
static function setMultiple($values){
foreach ($values as $key=>$value){
self::set($key, $value);
}
}
static function set($key, $value) {
$fullpath=self::getPath().'/'.$key.'.cache';
filedata::savedata($fullpath, $value);
}
static function has($key,$timeout=\null){
if ($timeout===\null)$timeout=isset(core::$data['cacheTimeout'])?core::$data['cacheTimeout']:self::$cacheTimeout;
$fullpath=self::getPath().'/'.$key.'.cache';
$mtime=@\filemtime($fullpath);
return !($mtime===\false or ($timeout>0 && \time() - $mtime > $timeout));
}
}