此文章是由jquery学堂1群里面的网友【流氓兔】提供的,他为了方便以后在网站上使用缓存功能,于是他就写了一个PHP缓存通用类,把有关缓存的功能方法通通封装成了一个文件缓存类,里面包括了8个方法,大家好好利用哇。
接下来简单的为大家介绍一下什么是缓存?
缓存是指临时文件交换区,电脑把最常用的文件从存储器里提出来临时放在缓存里,就像把工具和材料搬上工作台一样,这样会比用时现去仓库取更方便。因为缓存往往使用的是RAM(断电即掉的非永久储存),所以在忙完后还是会把文件送到硬盘等存储器里永久存储。电脑里最大的缓存就是内存条了,最快的是CPU上镶的L1和L2缓存,显卡的显存是给GPU用的缓存,硬盘上也有16M或者32M的缓存。千万不能把缓存理解成一个东西,它是一种处理方式的统称!
/*********************************************************************************
* 文件缓存类
*-------------------------------------------------------------------------------
* 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
*-------------------------------------------------------------------------------
* $Author:mashimaro
* $Dtime:2013-05-18
***********************************************************************************/
class filecacheInit {
private $cache_path = '.'; //缓存路径
private $cache_key = ""; //MD5信息
/**
* 文件缓存-设置缓存
* 设置缓存名称,数据,和缓存时间
* @param string $filename 缓存名
* @param array $data 缓存数据
*/
public function set_cache($filename, $data, $time = 0) {
$filename = $this->get_cache_filename($filename);
@file_put_contents($filename, '<?php exit;?>' . time() .'('.$time.')' . serialize($data));
clearstatcache();
return true;
}
/**
* 文件缓存-获取缓存
* 获取缓存文件,分离出缓存开始时间和缓存时间
* 返回分离后的缓存数据,解序列化
* @param string $filename 缓存名
* @return array
*/
public function get_cache($filename){
$filename = $this->get_cache_filename($filename);
/* 缓存不存在的情况 */
if (!file_exists($filename)) return false;
$data = file_get_contents($filename); //获取缓存
/* 缓存过期的情况 */
$filetime = substr($data, 13, 10);
$pos = strpos($data, ')');
$cachetime = substr($data, 24, $pos - 24);
$data = substr($data, $pos +1);
if ($cachetime == 0) return unserialize($data);
if (time() > ($filetime + $cachetime)) {
@unlink($filename);
return false; //缓存过期
}
return unserialize($data);
}
/**
* 文件缓存-清除缓存
* 删除缓存文件
* @param string $filename 缓存名
* @return array
*/
public function clear($filename){
$filename = $this->get_cache_filename($filename);
if (!file_exists($filename)) return true;
@unlink($filename);
return true;
}
/**
* 文件缓存-清除全部缓存
* 删除整个缓存文件夹文件,一般情况下不建议使用
* @param string $filename 缓存名
* @return array
*/
public function clear_all() {
@set_time_limit(3600);
$path = opendir($this->cache_path);
while (false !== ($filename = readdir($path))){
if ($filename !== '.' && $filename !== '..'){
@unlink($this->cache_path . '/' .$filename);
}
}
closedir($path);
return true;
}
/**
* 设置文件缓存路径
* 在配置文件中配置了该缓存文件
* $InitPHP_conf['cache']['filepath'] = 'data/filecache';
* @param string $path 路径
* @param string $key 需要md5信息
* @return string
*/
public function set_cache_path($path, $key = "") {
$this->cache_key = $key;
$this->cache_path = $path;
}
/**
* 获取缓存文件名
* @param string $filename 缓存名
* @return string
*/
private function get_cache_filename($filename){
//$tidmd5 = $this->cache_key? substr(md5($this->cache_key), 3) : substr(md5($this->cache_key), 3);
$fulldir = $this->cache_path."/";// .'/'. $tidmd5[0].'/'.$tidmd5[1].'/'.$tidmd5[2].'/';
$filename = $fulldir .md5($filename) . '.php';
if(!is_file($filename)){
if(!is_dir($fulldir)){
$this->dmkdir($fulldir);
}
}
return $filename;
}
/**
* 取得缓存路径
* @return string
*/
public function get_cache_path(){
$tidmd5 = substr(md5($this->cache_key), 3);
$fulldir = $this->cache_path .'/';//. $tidmd5[0].'/'.$tidmd5[1].'/'.$tidmd5[2].'/';
return $fulldir;
}
/**
* 创建目录
* @param string $dir 目录名称 可多层目录
* @param int $mode 目录权限
* @param bool $makeindex 是否生成index文件
* @return bool
*/
public function dmkdir($dir, $mode = 0777, $makeindex = TRUE){
if(!is_dir($dir)) {
$this -> dmkdir(dirname($dir), $mode,$makeindex);
@mkdir($dir, $mode);
}
return true;
}
}
如果您觉得本文的内容对您的学习有所帮助:
关键字:
php php缓存 php常用功能 php代码片段 php技巧 php开发 文件缓存