Home > Article > Backend Development > A PHP cache class, with three examples of Demo code_PHP tutorial
一个PHP缓存类,附三个实例Demo代码,本文由烈火小编收集于网络,首先我们看到的是cache.inc.php文件,请大家将以下代码保存为: cache.inc.php。
Copy to Clipboard引用的内容:[www.bkjia.com]The following are three example DEMOs. Please save the following codes as demo.php or other file names. Pay attention to the file path and make sure there is no mistake.
/*
* Can be freely reproduced and used, please keep the copyright information, thank you for using it!
* Class Name: Cache (For Php5)
* Version: 1.0
* Description: Dynamic cache class, used to control the page to automatically generate cache, call cache, update cache, and delete cache.
* Author: jiangjun8528@163.com, Junin
* Author Page: http://blog .csdn.Net/sdts/
* Source code from: Agni Download http://www.bkjia.com/down
* Last Modify: 2007-8-22
* Remark:
1. This version is the Php5 version. I have not written the Php4 version yet. If necessary, please refer to it and modify it yourself (it’s easier, don’t be so lazy, haha!).
2. This version is encoded in utf-8, if the website uses Please convert other codes by yourself. For Windows systems, use Notepad to open and save as, and select the corresponding code (generally ANSI). For Linux, please use the corresponding editing software or the iconv command line.
3. Don’t worry about the above paragraph if you copy and paste it. 2 items.
* Some thoughts about caching:
* The fundamental difference between dynamic caching and static caching is that it is automatic. The process of user accessing the page is the process of generating cache, browsing cache and updating cache. No manual work is required. Operational intervention.
* Static caching refers to generating static pages. Related operations are generally completed in the background of the website and require manual operation (that is, manual generation).
*/
/*
* Usage examples
--------------------------------Demo1----- -------------------------------------
require_once('cache.inc .php');
$cachedir = './Cache/'; //Set the cache directory
$cache = new Cache($cachedir,10); //Omit the parameters and use the default settings, $ cache = new Cache($cachedir);
if ($_GET['cacheact'] != 'rewrite') //Here is a trick, update the cache through xx.Php?cacheact=rewrite, and so on, You can also set some other operations
$cache->load(); //Load the cache. If the cache is valid, the following page code will not be executed
//The page code starts
echo date('H:i :s jS F');
//End of page code
$cache->write(); //First run or cache expiration, generate cache
------- --------------------------------Demo2----------------------------- -----------------------
require_once('cache.inc.php');
$cachedir = './Cache /'; //Set the cache directory
$cache = new Cache($cachedir,10); //Omit the parameters and use the default settings, $cache = new Cache($cachedir);
if ($ _GET['cacheact'] != 'rewrite') //Here is a trick, update the cache through xx.Php?cacheact=rewrite, and so on, you can also set some other operations
$cache-> load(); //Load the cache. If the cache is valid, the following page code will not be executed
//The page code starts
$content = date('H:i:s jS F');
echo $content ;
//End of page code
$cache->write(1,$content); //First run or cache expiration, generate cache
--------- ----------------------------Demo3---------------------- ---------------------
require_once('cache.inc.php');
define('CACHEENABLE',true);
if (CACHEENABLE) {
$cachedir = './Cache/'; //Set the cache directory
$cache = new Cache($cachedir,10); //Omit the parameters. Adopt the default settings, $cache = new Cache($cachedir);
if ($_GET['cacheact'] != 'rewrite') //This is a trick, update through xx.Php?cacheact=rewrite Cache, and so on, you can also set some other operations
$cache->load(); //Load cache, if the cache is valid, the following page code will not be executed
}
//Start of page code
$content = date('H:i:s jS F');
echo $content;
//End of page code
if (CACHEENABLE)
$cache->write (1,$content); //First run or cache expiration, generate cache
*/
?>