<? Php
/**
* Cache class
*/
Class Cache {
/**
* Cache path
*
* @ Var string
*/
Var $ cache_path;
/**
* Timeout
*
* @ Var integer
*/
Var $ time = 60;
/**
* Construct for this class
*
* @ Param string $ cache_path
* @ Return Cache
*/
Function Cache ($ cache_path = 'cache '){
If (is_dir ($ cache_path )){
$ This-> cache_path = rtrim ($ cache_path ,'/').'/';
} Else {
Die ('cache dir is not exists .');
}
}
/**
* Set timeout
*
* @ Param integer $ time
* @ Return boolean
*/
Function setTime ($ time ){
If (isset ($ time) & is_integer ($ time )){
$ This-> time = $ time;
Return true;
} Else {
Return false;
}
}
/**
* Read cache
*
* @ Param string $ cache_id
* @ Return mixed
*/
Function read ($ cache_id ){
$ Cache_file = $ this-> cache_path. $ cache_id. '. Caccache ';
If (! File_exists ($ cache_file )){
Return false;
}
$ Mtime = filemtime ($ cache_file );
If (time ()-$ mtime)> $ this-> time ){
Return false;
} Else {
$ Fp = fopen ($ cache_file, 'r ');
$ Content = fread ($ fp, filesize ($ cache_file ));
Fclose ($ fp );
Unset ($ fp );
If ($ content ){
Return unserialize ($ content );
} Else {
Return false;
}
}
}
/**
* Write cache in a file
*
* @ Param string $ content
* @ Param string $ cache_id
* @ Return boolean
*/
Function write ($ content, $ cache_id ){
$ Cache_file = $ this-> cache_path. $ cache_id. '. Caccache ';
If (file_exists ($ cache_file )){
@ Unlink ($ cache_file );
}
$ Fp = fopen ($ cache_file, 'w ');
$ Content = serialize ($ content );
If (fwrite ($ fp, $ content )){
Fclose ($ fp );
Unset ($ fp );
Return true;
} Else {
Fclose ($ fp );
Unset ($ fp );
Return false;
}
}
/**
* Clean all cache
*
* @ Param string $ path
* @ Return boolean
*/
Function cleanCache ($ path = 'cache '){
If (is_dir ($ path )){
$ Path = rtrim ($ path ,'/').'/';
$ Handler = opendir ($ path );
While ($ f = readdir ($ handler ))! = False ){
If (! Is_dir ($ f )){
If ($ f! = '.' & $ F! = '..'){
@ Unlink ($ path. $ f );
}
} Else {
$ This-> cleanCache ($ f );
}
}
} Else {
Return false;
}
}
}
?>