php缓存数据到本地缓存,PHP 的本地文件缓存处理类(非常高效) | 学步园

为了兼容服务器上没有安装memcache的内存缓存,专门开发了一个基于PHP5环境的,高效本地文件缓存类。

支持缓存失效时间的处理,并且非常节省内存。支持3个操作set(), get(),del(),详细使用方式请见备注中的example

/**

* php文件缓存类 FileCache

* @author Jerryli(hzjerry@gmail.com)

* @version V0.20130513

* @package

* @example

*

 
 

* $oFC = new CFileCache('./tmp/'); //创建文件缓存类

* $sKey = 'ab_123'; //缓存键值

* $data = $oFC->get($sKey); //取得缓存

* if(is_null($data))

*   $oFC->set($sKey, array('name'=>'ttt', 'datetime'=>date('Y-m-d H:i:s')), 10); //缓存不存在创建缓存

* print_r($data);

*

*/

final class CFileCache

{

/**

* 缓存目录

* @var string

*/

private static $msCachePath = null;

/**

* 默认缓存失效时间(1小时)

* @var int

*/

const miEXPIRE = 3600;

/**

* 构造

* self::$msCachePath 缓存目录为共享目录

* @param string $sCachePath

*/

function __construct($sCachePath='./tmp/')

{

if (is_null(self::$msCachePath))

self::$msCachePath = $sCachePath;

}

/**

* 读取缓存

* 返回: 缓存内容,字符串或数组;缓存为空或过期返回null

* @param string $sKey 缓存键值(无需做md5())

* @return string | null

* @access public

*/

public function get($sKey)

{

if(empty($sKey))

return false;

$sFile = self::getFileName($sKey);

if(!file_exists($sFile))

return null;

else

{

$handle = fopen($sFile,'rb');

if (intval(fgets($handle)) > time())//检查时间戳

{//未失效期,取出数据

$sData = fread($handle, filesize($sFile));

fclose($handle);

return unserialize($sData);

}

else

{//已经失效期

fclose($handle);

return null;

}

}

}

/**

* 写入缓存

*

* @param string $sKey 缓存键值

* @param mixed $mVal 需要保存的对象

* @param int $iExpire 失效时间

* @return bool

* @access public

*/

public function set($sKey, $mVal, $iExpire=null)

{

if(empty($sKey))

return false;

$sFile = self::getFileName($sKey);

if (!file_exists(dirname($sFile)))

if (!self::is_mkdir(dirname($sFile)))

return false;

$aBuf = array();

$aBuf[] = time() + ((empty($iExpire)) ? self::miEXPIRE : intval($iExpire));

$aBuf[] = serialize($mVal);

/*写入文件操作*/

$handle = fopen($sFile,'wb');

fwrite($handle, implode("\n", $aBuf));

fclose($handle);

return true;

}

/**

* 删除指定的缓存键值

*

* @param string $sKey 缓存键值

* @return bool

*/

public function del($sKey)

{

if(empty($sKey))

return false;

else

{

@unlink(self::getFileName($sKey));

return true;

}

}

/**

* 获取缓存文件全路径

* 返回: 缓存文件全路径

* $sKey的值会被转换成md5(),并分解为3级目录进行访问

* @param string $sKey 缓存键

* @return string

* @access protected

*/

private static function getFileName($sKey)

{

if(empty($sKey))

return false;

$key_md5 = md5($sKey);

$aFileName = array();

$aFileName[] = rtrim(self::$msCachePath,'/');

$aFileName[] = $key_md5{0} . $key_md5{1};

$aFileName[] = $key_md5{2} . $key_md5{3};

$aFileName[] = $key_md5{4} . $key_md5{5};

$aFileName[] = $key_md5;

return implode('/', $aFileName);

}

/**

* 创建目录

*

* @param string $sDir

* @return bool

*/

private static function is_mkdir($sDir='')

{

if(empty($sDir))

return false;

/*如果无法创建缓存目录,让系统直接抛出错误提示*/

if(!mkdir($sDir, 0666))

return false;

else

return true;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值