-
Notifications
You must be signed in to change notification settings - Fork 1
/
xcache.inc
47 lines (41 loc) · 1.48 KB
/
xcache.inc
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
<?php
// Avoid redeclare
define('XCACHED', true);
// Directory to save files, php need permission to write
$xdir='/var/xcache/';
function xcache_set($key, $val){
global $xdir;
$key=strtr(trim($key), [' '=>'_', '#'=>'', '/'=>'--', '.'=>'']);
$f=$xdir.substr($key,0,1);
if(!is_dir($f)) mkdir($f);
$val=var_export($val, true);
// Uncoment if you wish to use it for object store
//$val=str_replace('stdClass::__set_state', '(object)', $val);
file_put_contents($f.'/'.$key.'.inc', '<?php $val='."$val;\r\n", LOCK_EX);
}
function xcache_isset($key){
global $xdir;
$key=strtr(trim($key), [' '=>'-', '#'=>'', '/'=>'--', '.'=>'']);
$f=$xdir.substr($key,0,1);
return is_file($f.'/'.$key.'.inc')? filemtime($f.'/'.$key.'.inc'):false;
}
function xcache_get($key, $Revalidate=false){
global $xdir;
$key=strtr(trim($key), [' '=>'-', '#'=>'', '/'=>'--', '.'=>'']);
$f=$xdir.substr($key,0,1);
if(!$Revalidate) @include $f.'/'.$key.'.inc';
else eval('?>'.file_get_contents($f.'/'.$key.'.inc'));
return isset($val)? $val:false;
}
function xcache_unset($key){
global $xdir;
$key=strtr(trim($key), [' '=>'-', '#'=>'', '/'=>'--', '.'=>'']);
$f=$xdir.substr($key,0,1);
unlink($f.'/'.$key.'.inc');
}
function xcache_unset_by_prefix($key){
global $xdir;
$key=strtr(trim($key), [' '=>'-', '#'=>'', '/'=>'--', '.'=>'']);
$f=$xdir.substr($key,0,1);
exec('rm -rf '.$f.'/'.$key.'*');
}