A Simple and Practical Complete Example of PHP Cache Class
- 2021-07-09 07:33:06
- OfStack
This paper describes a simple and practical PHP cache class, which can be used to check whether the cache file is within the set update time, clear the cache file, generate the cache file name according to the current dynamic file, create the directory continuously, and output the cache file statically. To use PHP to develop CMS system, it is necessary to deal with cache. Reasonable use of cache can effectively improve the efficiency of program execution.
The complete code for the php cache class file is as follows:
<?php
/*
* Cache class cache
*/
class cache {
// Cache directory
var $cacheRoot = "./cache/";
// Cache update time seconds, 0 Is not cached
var $cacheLimitTime = 0;
// 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 within the set update time
* Return: If it is within the update time, the file contents will be returned, otherwise, it will fail
*/
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 files 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 the cache file
* string $fileName Specify a file name ( Containing function ) Or all (All)
* Return: Clear successfully returns true Otherwise, return false
*/
function clearCache( $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 from the current dynamic file */
function getCacheFileName() {
return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
}
/*
* Cache file establishment time
* string $fileName Cache file name (including relative path)
* Return: The number of seconds when the file was generated, and the file did not exist. Return 0
*/
function getFileCreateTime( $fileName ) {
if( ! trim($fileName) ) return 0;
if( file_exists( $fileName ) ) {
return intval(filemtime( $fileName ));
}else return 0;
}
/*
* Save a file
* string $fileName File name (including relative path)
* string $text File content
* Return: Successful return ture Failure returns false
*/
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;
}
/*
* Continuous catalogue building
* string $dir Directory string
* int $mode Authority number
* Return: Successfully created or all built returns true Return in other ways 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 as follows:
include( "cache.php" );
$cache = new cache(30);
$cache->cacheCheck();
echo date("Y-m-d H:i:s");
$cache->caching();