Repository navigation
Expand file tree
/
Copy pathcache.class.php
More file actions
70 lines (58 loc) · 1.72 KB
/
Copy pathcache.class.php
File metadata and controls
70 lines (58 loc) · 1.72 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
<?php
class cache {
var $db;
var $cachefile;
function cache(& $db) {
$this->db=$db;
}
function getfile($cachename) {
$this->cachefile=APP_ROOT.'/data/cache/'.$cachename.'.php';
}
function isvalid($cachename,$cachetime) {
if(0==$cachetime) {
return true;
}
$this->getfile($cachename);
if(!is_readable($this->cachefile)||$cachetime<0) {
return false;
}
clearstatcache();
return (time()-filemtime($this->cachefile))<$cachetime;
}
function read($cachename,$cachetime=0) {
$this->getfile($cachename);
if($this->isvalid($cachename,$cachetime)) {
return @include $this->cachefile;
}
return false;
}
function write($cachename, $arraydata) {
$this->getfile($cachename);
if(!is_array($arraydata)) return false;
$strdata = "<?php\nreturn ".var_export($arraydata, true).";\n?>";
$bytes = writetofile($this->cachefile, $strdata);
return $bytes;
}
function remove($cachename) {
$this->getfile($cachename);
if(file_exists($this->cachefile)) {
unlink($this->cachefile);
}
}
function load($cachename,$id='id') {
$arraydata=$this->read($cachename);
if(!$arraydata) {
$cursor = $this->db->selectCollection($cachename)->find();
foreach($cursor as $item) {
if( isset($item['k']) ) {
$arraydata[$item['k']]=$item['v'];
}else {
$arraydata[$item[$id]]=$item;
}
}
$this->write($cachename,$arraydata);
}
return $arraydata;
}
}
?>