The following is a PHP cache file implementation class. According to my experience, the cache file is compared with the time set by the user, the day when the file was generated, and the current time, and then it is determined whether the cache file needs to be regenerated. */
The following is a PHP tutorial cache file implementation class. According to my experience, the cache file is compared with the time set by the user, the day when the file was generated, and the current time, and then it is determined whether the cache file needs to be regenerated. */
class pagecache {
/**
* @var string $file cache file address
* @access public
*/
public $file;
/**
* @var int $cachetime cache time
* @access public
*/
public $cachetime = 3600;
/**
* Constructor
* @param string $file cache file address
* @param int $cachetime cache time
*/
function __construct($file, $cachetime = 3600) {
$this->file = $file;
$this->cachetime = $cachetime;
}
/**
* Get the cached content
* @param bool Whether to output directly, true to go directly to the cache page, false to return the cached content
* @return mixed
*/
public function get($output = true) {
if (is_file($this ->file) && (time()-filemtime($this->file))<=$this->cachetime && !$_get['nocache']) {
if ($output) {
header('location:' . $this->file);
exit;
} else {
return file_get_contents($this->file);
}
} else {
return false;
}
}
/**
* Set cache content
* @param $content content html string
*/
public function set($content) {
$fp = fopen( $this->file, 'w');
fwrite($fp, $content);
fclose($fp);
}
}