php文件缓存技术实现代码
2013-09-05 19:25
585 查看
下面是一个php缓存文件实现类,根据我的经验,缓存文件是根据用户设置的时间与文件生成的日间以及当前时间进行比较,然后再判断是否需要重新生成缓存文件。*/下面是一个php教程缓存文件实现类,根据我的经验,缓存文件是根据用户设置的时间与文件生成的日间以及当前时间进行比较,然后再判断是否需要重新生成缓存文件。*/
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))<=$this->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);
}
}
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))<=$this->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);
}
}
相关文章推荐
- php文件缓存技术实现代码
- PHP Memcached + APC + 文件缓存封装实现代码
- php实现文件数据缓存实现代码
- PHP Memcached + APC + 文件缓存封装实现代码
- php中文件缓存实现程序代码
- php实现多文件上传代码
- PHP 遍历文件实现代码
- PHPExcel读取Excel文件的实现代码
- PHP 图片文件上传实现代码
- php中将数组存到文件里的实现代码
- php+mysql缓存技术的实现
- [转]缓存技术详谈和代码实现
- 用PHP实现维护文件代码
- php利用iframe实现无刷新文件上传功能的代码
- php删除与复制文件夹及其文件夹下所有文件的实现代码
- 基于PHP的AJAX技术实现文件异步上传
- php 批量生成html,txt文件的实现代码
- 三步FTP实现PHP上传文件代码剖析
- PHP 缓存实现代码及详细注释