Home > Backend Development > PHP Tutorial > PHP file caching technology implementation code_PHP tutorial

PHP file caching technology implementation code_PHP tutorial

WBOY
Release: 2016-07-20 11:09:07
Original
725 people have browsed it

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);
}
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444822.htmlTechArticleThe following is a php cache file implementation class. According to my experience, the cache file is based on the time and file set by the user. Compare the generated daytime with the current time, and then determine whether...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template