Simple and practical website PHP cache class instance
- 2021-07-09 07:16:45
- OfStack
Cache technology is widely used in practical use, which can effectively reduce the pressure of accessing the server database and improve the running speed. At present, many CMS content management systems frequently use caching mechanism to improve the efficiency of system operation. This paper takes a simple and practical cache class as an example to help you refer to the cache mechanism and writing.
The code for the cache file cache. php is as follows:
<?php
/*
Users need predefined constants:
_CachePath_ Template cache path
_CacheEnable_ Whether the automatic caching mechanism is turned on, undefined, or empty indicates that the automatic caching mechanism is turned off
_ReCacheTime_ Time interval between automatic re-caching, in seconds, undefined or empty, indicating that automatic re-caching is turned off
*/
class cache
{
var $cachefile;
var $cachefilevar;
function cache()
{
// Object for the current page Cache Group file name $this->cachefilevar And file name $this->cachefile
// Parameters of dynamic pages correspond to different Cache Files are also different, but each 1 All of the dynamic pages Cache Files all have the same file name, but the extension is different
$s=array(".","/");$r=array("_","");
$this->cachefilevar=str_replace($s,$r,$_SERVER["SCRIPT_NAME"])."_".$_GET[_ActionVar_];
$this->cachefile=$this->cachefilevar.".".md5($_SERVER["REQUEST_URI"]);
}
// Delete the current page / Caching of modules
function delete()
{
// Delete the cache of the current page
$d = dir(_CachePath_);
$strlen=strlen($this->cachefilevar);
// Returns all Taiyuan of the current page 264 Hospital Cache Filegroup
while (false !== ($entry = $d->read()))
{
if (substr($entry,0,$strlen)==$this->cachefilevar)
{
if (!unlink(_CachePath_."/".$entry)) {echo "Cache Directory cannot be written to ";exit;}
}
}
}
// Determine whether it has Cache Pass, and whether it is necessary Cache
function check()
{
// If you set the cache update interval _ReCacheTime_
if (_ReCacheTime_+0>0)
{
// Return to the current page Cache Last update time of
$var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];
// Delete if the update time exceeds the update interval time Cache Documents
if (time()-$var>_ReCacheTime_)
{
$this->delete();$ischage=true;
}
}
// Returns the of the current page Cache
$file=_CachePath_."/".$this->cachefile;
// Determine the current page Cache Does it exist And Cache Is the function turned on
return (file_exists($file) and _CacheEnable_ and !$ischange);
}
// Read Cache
function read()
{
// Returns the of the current page Cache
$file=_CachePath_."/".$this->cachefile;
// Read Cache Contents of the file
if (_CacheEnable_) return readfile($file);
else return false;
}
// Generate Cache
function write($output)
{
// Returns the of the current page Cache
$file=_CachePath_."/".$this->cachefile;
// If Cache Function on
if (_CacheEnable_)
{
// Writes the output to the Cache Documents
$fp=@fopen($file,'w');
if (!@fputs($fp,$output)) {echo " Template Cache Write failure ";exit;}
@fclose($fp);
// If you set the cache update interval _ReCacheTime_
if (_ReCacheTime_+0>0)
{
// Update the current page Cache Last update time of
$file=_CachePath_."/".$this->cachefilevar;
$fp=@fopen($file,'w');
if (!@fwrite($fp,time())) {echo "Cache Directory cannot be written to ";exit;}
@fclose($fp);
}
}
}
}
?>
Use of cache classes:
<?php
define("_CachePath_","./cache/");
define("_CacheEnable_","1");
define("_ReCacheTime_","43200");
include('cache.php');
$cache=new cache();
if ($cache->check())
{
$template=$cache->read();
}
else
{
ob_start();
ob_implicit_flush(0);
?>
Here is the page content. . . .
<?php
$template = ob_get_contents();
$cache->write($template);
}
?>