欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 缓存文件入门程序_PHP教程

程序员文章站 2022-03-25 09:23:02
...
class PageCache {

/**
* @var string $file 缓存文件地址
* @access public
*/
public $file;

/**
* @var int $cacheTime 缓存时间
* @access public
*/
public $cacheTime = 3600;

/**
* 构造函数
* @param string $file 缓存文件地址
* @param int $cacheTime 缓存时间
*/
function __construct($file, $cacheTime = 3600) {
$this->file = $file;
$this->cacheTime = $cacheTime;
}

/**
* 取缓存内容
* @param bool 是否直接输出,true直接转到缓存页,false返回缓存内容
* @return mixed
*/
public function get($output = true) {
if (is_file($this->file) && (time()-filemtime($this->file))cacheTime && !$_GET['nocache']) {
if ($output) {
header('location:' . $this->file);
exit;
} else {
return file_get_contents($this->file);
}
} else {
return false;
}
}

/**
* 设置缓存内容
* @param $content 内容html字符串
*/
public function set($content) {
$fp = fopen($this->file, 'w');
fwrite($fp, $content);
fclose($fp);
}
}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445011.htmlTechArticleclass PageCache { /** * @var string $file 缓存文件地址 * @access public */ public $file; /** * @var int $cacheTime 缓存时间 * @access public */ public $cacheTime = 3600;...