|
<? Php
/**
* Simple File Cache class
*
*/
Class XZCache {
// Default cache time one hour
Var $ cache_time = 3600;
// Default cache dir
Var $ cache_dir = './cache ';
Public function _ construct ($ cache_dir = null, $ cache_time = null ){
$ This-> cache_dir = isset ($ cache_dir )? $ Cache_dir: $ this-> cache_dir;
$ This-> cache_time = isset ($ cache_time )? $ Cache_time: $ this-> cache_time;
}
Public function saveCache ($ key, $ value ){
If (is_dir ($ this-> cache_dir )){
$ Cache_file = $ this-> cache_dir. '/xzcache _'. md5 ($ key );
$ Timedif = @ (time ()-filemtime ($ cache_file ));
If ($ timedif >=$ this-> cache_time ){
// Cached file is too old, create new
$ Serialized = serialize ($ value );
If ($ f = @ fopen ($ cache_file, 'w ')){
Fwrite ($ f, $ serialized, strlen ($ serialized ));
Fclose ($ f );
}
}
$ Result = 1;
} Else {
Echo "Error: dir is not exist .";
$ Result = 0;
}
Return $ result;
}
/**
* @ Return array
* 0 no cache
* 1 cached
* 2 overdue
*/
Public function getCache ($ key ){
$ Cache_file = $ this-> cache_dir. '/xzcache _'. md5 ($ key );
If (is_dir ($ this-> cache_dir) & is_file ($ cache_file )){
$ Timedif = @ (time ()-filemtime ($ cache_file ));
If ($ timedif >=$ this-> cache_time ){
$ Result ['cached'] = 2;
} Else {
// Cached file is fresh enough, return cached array
$ Result ['value'] = unserialize (file_get_contents ($ cache_file ));
$ Result ['cached'] = 1;
}
} Else {
Echo "Error: no cache ";
$ Result ['cached'] = 0;
}
Return $ result;
}
} // End of class
|