A simple and practical PHP cache complete instance, simple and practical php Cache

Source: Internet
Author: User
Tags php mysql

A simple and practical PHP cache complete instance, simple and practical php Cache

This article describes a simple and practical PHP cache class, it can be used to check whether cached files are updated within the set time, clear cached files, generate cache file names based on the current dynamic files, create directories consecutively, and output static cache files. For the CMS system developed using PHP, it is essential to handle the cache and make good use of the cache can effectively improve the program execution efficiency.

The complete php cache file code is as follows:

<? Php/** cache class cache */class cache {// cache directory var $ cacheRoot = ". /cache/"; // The cache Update time in seconds. 0 indicates that no cache var $ cacheLimitTime = 0; // The cache file name var $ cacheFileName = ""; // cache extension var $ cacheFileExt = "php";/** constructor * int $ cacheLimitTime cache Update time */function cache ($ cacheLimitTime) {if (intval ($ cacheLimitTime) $ this-> cacheLimitTime = $ cacheLimitTime; $ this-> cacheFileName = $ this-> getCacheFileName (); ob_start ();} /** check whether the cache file is updated * Return within the specified period: if the file content is returned within the update time, otherwise, the system returns failure */function cacheCheck () {if (file_exists ($ this-> cacheFileName )) {$ cTime = $ this-> getFileCreateTime ($ this-> cacheFileName); if ($ cTime + $ this-> cacheLimitTime> time ()) {echo file_get_contents ($ this-> cacheFileName); ob_end_flush (); exit ;}} return false ;} /** cache file or output static * string $ staticFileName static file name (including relative path) */function caching ($ staticFileName = "") {if ($ This-> cacheFileName) {$ cacheContent = ob_get_contents (); ob_end_flush (); if ($ staticFileName) {$ this-> saveFile ($ staticFileName, $ cacheContent );} if ($ this-> cacheLimitTime) $ this-> saveFile ($ this-> cacheFileName, $ cacheContent );}} /** clear cache file * string $ fileName specify the file name (including the function) or all (all) * returns true if the file is cleared successfully, otherwise, false */function compute AchE ($ fileName = "all") {if ($ fileName! = "All") {$ fileName = $ this-> cacheRoot. strtoupper (md5 ($ fileName )). ". ". $ this-> cacheFileExt; if (file_exists ($ fileName) {return @ unlink ($ fileName);} else return false;} if (is_dir ($ this-> cacheRoot )) {if ($ dir = @ opendir ($ this-> cacheRoot) {while ($ file = @ readdir ($ dir) {$ check = is_dir ($ file ); if (! $ Check) @ unlink ($ this-> cacheRoot. $ file) ;}@ closedir ($ dir); return true ;}else {return false ;}} else {return false ;}} /* generate the cache file name based on the current dynamic file */function getCacheFileName () {return $ this-> cacheRoot. strtoupper (md5 ($ _ SERVER ["REQUEST_URI"]). ". ". $ this-> cacheFileExt;}/** cache file creation time * string $ fileName cache file name (including relative path) * return: file generation time in seconds, 0 */function getFileCreateTime ($ fileName) {if (! Trim ($ fileName) return 0; if (file_exists ($ fileName) {return intval (filemtime ($ fileName);} else return 0 ;} /** save the file * string $ fileName file name (including relative path) * string $ text file content * return: True is returned successfully, false is returned if a failure */function saveFile ($ fileName, $ text) {if (! $ FileName |! $ Text) return false; if ($ this-> makeDir (dirname ($ fileName) {if ($ fp = fopen ($ fileName, "w ")) {if (@ fwrite ($ fp, $ text) {fclose ($ fp); return true ;}else {fclose ($ fp); return false ;}}} return false;}/** create directory consecutively * string $ dir directory string * int $ mode Permission Number * return: true if the directory is created successfully or all has been created, otherwise, false */function makeDir ($ dir, $ mode = "0777") {if (! $ Dir) return 0; $ dir = str_replace ("\", "/", $ dir); $ mdir = ""; foreach (explode ("/", $ dir) as $ val) {$ mdir. = $ val. "/"; if ($ val = ".. "| $ val = ". "| trim ($ val) =" ") continue; if (! File_exists ($ mdir) {if (! @ Mkdir ($ mdir, $ mode) {return false ;}} return true ;}}?>

When using this cache class, you can save the above Code as cache. php. The specific usage is as follows:

include( "cache.php" );$cache = new cache(30);$cache->cacheCheck();echo date("Y-m-d H:i:s");$cache->caching();

Php Data Cache

One is the cache application server-level software for page results, such as squid

Memcached is a memory-level data software that frequently calls php and consumes a large amount of resources for each query.

One is the cache for php code optimization, such as apc, eAccelerator, and XCache in apache.

There is also a File Cache, which is generally implemented by php itself.

See what your requirements are. If you have any questions, contact 1465663870.

I want to develop a website with PHP programming code. How can I implement PHP MYSQL cache? For example

// The following are cache classes:
<? Php
Class cache {
// Cache directory
Var $ cacheRoot = "./cache /";
// Cache Update time in seconds. 0 indicates no cache.
Var $ cacheLimitTime = 0;
// Cache file name
Var $ cacheFileName = "";
// Cache Extension
Var $ cacheFileExt = "html ";

/*
* Constructor
* Int $ cacheLimitTime cache Update Time
*/
Function cache ($ cacheLimitTime ){
If (intval ($ cacheLimitTime ))
$ This-> cacheLimitTime = $ cacheLimitTime;
$ This-> cacheFileName = $ this-> getCacheFileName ();
// Echo $ this-> cacheFileName;
Ob_start ();
}

/*
* Check whether the cached files are updated within the specified Update time.
* Return: if the file content is returned within the update time, otherwise, an error is returned.
*/
Function cacheCheck (){
If (file_exists ($ this-> cacheFileName )){
$ CTime = $ this-> getFileCreateTime ($ this-> cacheFileName );
If ($ cTime + $ this-> cacheLimitTime> time ()){
Echo file_get_contents ($ this-> cacheFileName );
Ob_end_flush ();
Exit;
}
}
Return false;
}

/*
* Cache file or output static
* String $ staticFileName static file name (including relative path)
*/
Function caching ($ staticFileName = ""){
If ($ this-> cacheFileName ){
$ CacheContent = ob_get_contents ();
// Echo $ cacheContent;
Ob_end_flush ();

If ($ staticFileName ){
$ This-> saveFile ($ staticFileName, $ cacheContent );
}

If ($ this-> cacheLimitTime)
$ This-> saveFile ($ this-> cacheFileName, $ cacheContent );
}
}

/*
... The remaining full text>

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.