Php Data caching program _ PHP Tutorial

Source: Internet
Author: User
Php implements the data cache program .? Php *** cacheclass * classCache {*** cachepath *** @ varstring * var $ cache_path; *** timeout *** @ varinteger * var $ time60; * ** constructforthisclass ** @ p /**
* 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;
}
}
}

?>

Http://www.bkjia.com/PHPjc/630437.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630437.htmlTechArticle? Php/*** cache class */class Cache {/*** cache path ** @ var string */var $ cache_path; /*** timeout *** @ var integer */var $ time = 60;/*** construct for this class *** @ p...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.