分享:php文件缓存类
发布时间:2017-03-17 编辑:jiaochengji.com
本文分享一个php实现的文件缓存类,比较简单,可以学习下php文件缓存的原理与实现方法,有需要的朋友参考下。
本节内容:
一例php文件缓存类的代码
例子:
复制代码 代码示例:
<?php
/**
* 文件缓存类
* @author flynetcn
* @site wwww.jbxue.com
*/
class FileCache
{
const CACHE_DIR = '/tmp/cache';
private $dir;
private $expire = 300;
/**
* @param $strDir 缓存子目录(方便清cache)
*/
function __construct($strDir='')
{
if ($strDir) {
$this->dir = self::CACHE_DIR.'/'.$strDir;
}
}
private function getFileName($strKey)
{
return $this->dir.'/'.$strKey;
}
public function setExpire($intSecondNum)
{
if ($intSecondNum<10 || !is_int($intSecondNum)) {
return false;
}
$this->expire = $intSecondNum;
return true;
}
public function getExpire()
{
return $this->expire;
}
public function get($strKey)
{
$strFileName = $this->getFileName($strKey);
if (!@file_exists($strFileName)) {
return false;
}
if (filemtime($strFileName) < (time()-$this->expire)) {
return false;
}
$intFileSize = filesize($strFileName);
if ($intFileSize == 0) {
return false;
}
if (!$fp = @fopen($strFileName, 'rb')) {
return false;
}
flock($fp, LOCK_SH);
$cacheData = unserialize(fread($fp, $intFileSize));
flock($fp, LOCK_UN);
fclose($fp);
return $cacheData;
}
public function set($strKey, $cacheData)
{
if (!is_dir($this->dir)) {
if (!@mkdir($this->dir, 0755, true)) {
trigger_error("mkdir({$this->dir}, 0755, true) failed", E_USER_ERROR);
return false;
}
}
$strFileName = $this->getFileName($strKey);
if (@file_exists($strFileName)) {
$intMtime = filemtime($strFileName);
} else {
$intMtime = 0;
}
if (!$fp = fopen($strFileName,'wb')) {
return false;
}
if (!flock($fp, LOCK_EX+LOCK_NB)) {
fclose($fp);
return false;
}
if (time() - $intMtime < 1) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
fseek($fp, 0);
ftruncate($fp, 0);
fwrite($fp, serialize($cacheData));
flock($fp, LOCK_UN);
fclose($fp);
@chmod($strFileName, 0755);
return true;
}
}
/**
* 文件缓存类
* @author flynetcn
* @site wwww.jbxue.com
*/
class FileCache
{
const CACHE_DIR = '/tmp/cache';
private $dir;
private $expire = 300;
/**
* @param $strDir 缓存子目录(方便清cache)
*/
function __construct($strDir='')
{
if ($strDir) {
$this->dir = self::CACHE_DIR.'/'.$strDir;
}
}
private function getFileName($strKey)
{
return $this->dir.'/'.$strKey;
}
public function setExpire($intSecondNum)
{
if ($intSecondNum<10 || !is_int($intSecondNum)) {
return false;
}
$this->expire = $intSecondNum;
return true;
}
public function getExpire()
{
return $this->expire;
}
public function get($strKey)
{
$strFileName = $this->getFileName($strKey);
if (!@file_exists($strFileName)) {
return false;
}
if (filemtime($strFileName) < (time()-$this->expire)) {
return false;
}
$intFileSize = filesize($strFileName);
if ($intFileSize == 0) {
return false;
}
if (!$fp = @fopen($strFileName, 'rb')) {
return false;
}
flock($fp, LOCK_SH);
$cacheData = unserialize(fread($fp, $intFileSize));
flock($fp, LOCK_UN);
fclose($fp);
return $cacheData;
}
public function set($strKey, $cacheData)
{
if (!is_dir($this->dir)) {
if (!@mkdir($this->dir, 0755, true)) {
trigger_error("mkdir({$this->dir}, 0755, true) failed", E_USER_ERROR);
return false;
}
}
$strFileName = $this->getFileName($strKey);
if (@file_exists($strFileName)) {
$intMtime = filemtime($strFileName);
} else {
$intMtime = 0;
}
if (!$fp = fopen($strFileName,'wb')) {
return false;
}
if (!flock($fp, LOCK_EX+LOCK_NB)) {
fclose($fp);
return false;
}
if (time() - $intMtime < 1) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
fseek($fp, 0);
ftruncate($fp, 0);
fwrite($fp, serialize($cacheData));
flock($fp, LOCK_UN);
fclose($fp);
@chmod($strFileName, 0755);
return true;
}
}
您可能感兴趣的文章:
超级精练的php缓存类与实例
php简单页面缓存的实现代码
采用PEAR来缓冲PHP程序
PHP模板引擎Smarty缓存使用
PHP强制更新图片缓存的示例代码
php 文件缓存数据类的代码分享
php加速工具eAccelerator 配置参数详解
php加速器eAccelerator配置参数详解
一个php的页面缓存类
php生成静态文件的二种方法
上一篇:PHP Socket实现的POP3类
下一篇:PHP的魔术方法与魔术常量
[关闭]