最新标注
热门标注
历史更新
站点地图
RSS
Home
CrazyCoder
»
原创
»
it评论
»
it业界
»
互联网
»
精品软件
开发语言
»
网络编程
编程综合
嵌入式开发
算法
英文资料
汇编语言
PowerBuilder
p2p技术
验证码识别
DotNet
Java教程
C语言教程
C 教程
Delphi教程
VB教程
QQ协议开发
项目管理
»
数据库
»
Web开发
»
Python
Css教程
XML教程
网页特效
PhotoShop教程
Illustrator教程
CorelDraw教程
Ruby教程
CorelDraw教程
网站安全
开发平台-工具
Web
PHP教程
Flash教程
Ajax教程
Javascript教程
Html教程
Dreamweaver
Asp教程
技术综合
»
软件测试
办公软件
QQ相关
Windows
网络技术
游戏开发
软件工程
软件教程
编程思想
游戏开发
外挂开发
OpenApi
AutoCad
广告设计
3D设计
平面设计相关
移动手机开发
服务器
»
Linux
Unix/FreeBsd
web服务器
服务器技术
安全
博文摘选
»
翻译
首页
»
PHP教程
» php缓存技术,PHP缓存技术实现
Rss订阅
php缓存技术,PHP缓存技术实现
on 2010-12-9
in
PHP教程
|
0 Comment
发个
工具
。
参考shindig的缓存类和apc。
Php代码
<?php
class
CacheException
extends
Exception {}
/**
* 缓存抽象类
*/
abstract
class
Cache_Abstract {
/**
* 读缓存变量
*
* @param string $key 缓存下标
* @return mixed
*/
abstract
public
function
fetch(
$key
);
/**
* 缓存变量
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
abstract
public
function
store(
$key
,
$value
);
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract
public
function
delete
(
$key
);
/**
* 清(删)除所有缓存
*
* @return Cache_Abstract
*/
abstract
public
function
clear();
/**
* 锁定缓存变量
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract
public
function
lock(
$key
);
/**
* 缓存变量解锁
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract
public
function
unlock(
$key
);
/**
* 取得缓存变量是否被锁定
*
* @param string $key 缓存下标
* @return bool
*/
abstract
public
function
isLocked(
$key
);
/**
* 确保不是锁定状态
* 最多做$tries次睡眠等待解锁,超时则跳过并解锁
*
* @param string $key 缓存下标
*/
public
function
checkLock(
$key
) {
if
(!
$this
->isLocked(
$key
)) {
return
$this
;
}
$tries
= 10;
$count
= 0;
do
{
usleep(200);
$count
++;
}
while
(
$count
<=
$tries
&&
$this
->isLocked(
$key
));
// 最多做十次睡眠等待解锁,超时则跳过并解锁
$this
->isLocked(
$key
) &&
$this
->unlock(
$key
);
return
$this
;
}
}
/**
* APC扩展缓存实现
*
*
* @category Mjie
* @package Cache
* @author 流水孟春
* @copyright Copyright (c) 2008- <cmpan(at)qq.com>
* @license New BSD License
* @version $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $
*/
class
Cache_Apc
extends
Cache_Abstract {
protected
$_prefix
=
'cache.mjie.net'
;
public
function
__construct() {
if
(!function_exists(
'apc_cache_info'
)) {
throw
new
CacheException(
'apc extension didn\'t installed'
);
}
}
/**
* 保存缓存变量
*
* @param string $key
* @param mixed $value
* @return bool
*/
public
function
store(
$key
,
$value
) {
return
apc_store(
$this
->_storageKey(
$key
),
$value
);
}
/**
* 读取缓存
*
* @param string $key
* @return mixed
*/
public
function
fetch(
$key
) {
return
apc_fetch(
$this
->_storageKey(
$key
));
}
/**
* 清除缓存
*
* @return Cache_Apc
*/
public
function
clear() {
apc_clear_cache();
return
$this
;
}
/**
* 删除缓存单元
*
* @return Cache_Apc
*/
public
function
delete
(
$key
) {
apc_delete(
$this
->_storageKey(
$key
));
return
$this
;
}
/**
* 缓存单元是否被锁定
*
* @param string $key
* @return bool
*/
public
function
isLocked(
$key
) {
if
((apc_fetch(
$this
->_storageKey(
$key
) .
'.lock'
)) === false) {
return
false;
}
return
true;
}
/**
* 锁定缓存单元
*
* @param string $key
* @return Cache_Apc
*/
public
function
lock(
$key
) {
apc_store(
$this
->_storageKey(
$key
) .
'.lock'
,
''
, 5);
return
$this
;
}
/**
* 缓存单元解锁
*
* @param string $key
* @return Cache_Apc
*/
public
function
unlock(
$key
) {
apc_delete(
$this
->_storageKey(
$key
) .
'.lock'
);
return
$this
;
}
/**
* 完整缓存名
*
* @param string $key
* @return string
*/
private
function
_storageKey(
$key
) {
return
$this
->_prefix .
'_'
.
$key
;
}
}
/**
* 文件缓存实现
*
*
* @category Mjie
* @package Cache
* @author 流水孟春
* @copyright Copyright (c) 2008- <cmpan(at)qq.com>
* @license New BSD License
* @version $Id: Cache/File.php 版本号 2010-04-18 16:46 cmpan $
*/
class
Cache_File
extends
Cache_Abstract {
public
$useSubdir
= false;
protected
$_cachesDir
=
'cache'
;
public
function
__construct() {
if
(defined(
'DATA_DIR'
)) {
$this
->_setCacheDir(DATA_DIR .
'/cache'
);
}
}
/**
* 获取缓存文件
*
* @param string $key
* @return string
*/
protected
function
_getCacheFile(
$key
) {
$subdir
=
$this
->useSubdir ?
substr
(
$key
, 0, 2) .
'/'
:
''
;
return
$this
->_cachesDir .
'/'
.
$subdir
.
$key
.
'.php'
;
}
/**
* 读取缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头
*
* @param string $key 缓存下标
* @return mixed
*/
public
function
fetch(
$key
) {
$cacheFile
= self::_getCacheFile(
$key
);
if
(
file_exists
(
$cacheFile
) &&
is_readable
(
$cacheFile
)) {
// include 方式
//return include $cacheFile;
// 系列化方式
return
unserialize(@
file_get_contents
(
$cacheFile
, false, NULL, 13));
}
return
false;
}
/**
* 缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
public
function
store(
$key
,
$value
) {
$cacheFile
= self::_getCacheFile(
$key
);
$cacheDir
= dirname(
$cacheFile
);
if
(!
is_dir
(
$cacheDir
)) {
if
(!@
mkdir
(
$cacheDir
, 0755, true)) {
throw
new
CacheException(
"Could not make cache directory"
);
}
}
// 用include方式
//return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');
return
@
file_put_contents
(
$cacheFile
,
'<?php exit;?>'
. serialize(
$value
));
}
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return Cache_File
*/
public
function
delete
(
$key
) {
if
(
empty
empty
(
$key
)) {
throw
new
CacheException(
"Missing argument 1 for Cache_File::delete()"
);
}
$cacheFile
= self::_getCacheFile(
$key
);
if
(!@unlink(
$cacheFile
)) {
throw
new
CacheException(
"Cache file could not be deleted"
);
}
return
$this
;
}
/**
* 缓存单元是否已经锁定
*
* @param string $key
* @return bool
*/
public
function
isLocked(
$key
) {
$cacheFile
= self::_getCacheFile(
$key
);
clearstatcache();
return
file_exists
(
$cacheFile
.
'.lock'
);
}
/**
* 锁定
*
* @param string $key
* @return Cache_File
*/
public
function
lock(
$key
) {
$cacheFile
= self::_getCacheFile(
$key
);
$cacheDir
= dirname(
$cacheFile
);
if
(!
is_dir
(
$cacheDir
)) {
if
(!@
mkdir
(
$cacheDir
, 0755, true)) {
if
(!
is_dir
(
$cacheDir
)) {
throw
new
CacheException(
"Could not make cache directory"
);
}
}
}
// 设定缓存锁文件的访问和修改时间
@touch(
$cacheFile
.
'.lock'
);
return
$this
;
}
/**
* 解锁
*
* @param string $key
* @return Cache_File
*/
public
function
unlock(
$key
) {
$cacheFile
= self::_getCacheFile(
$key
);
@unlink(
$cacheFile
.
'.lock'
);
return
Tags:
php静态缓存
php页面缓存
php禁止缓存
php不缓存
php缓存
php缓存技术
延伸阅读
2010-12-9
--
php输出缓存,PHP禁止页面缓存输出
2010-12-9
--
php缓存,PHP缓存详谈
2010-12-9
--
php性能测试,PHP 文件缓存的性能测试
2010-12-9
--
php技术,PHP安全技术之 实现php基本安全
2010-12-15
--
php输出缓存,PHP程序加速探索之缓存输出
2011-4-26
--
静态缓存,asp伪静态情况下实现的utf-8文件缓存实现代码
2011-1-27
--
php缓存,PHP企业级应用之常见缓存技术篇
2009-1-12
--
phpoop:PHP高级OOP技术演示
2008-12-30
--
javascript缓存:PHP-Javascript 返回上一页 无缓存Cache问题
2009-1-12
--
生成php缓存文件:PHP文件缓存Cache的性能测试
最新评论
发表评论
昵称
评论
验证码
点击图片更换
赞助商广告
随机更新
photoshop教程
c语言输入,C语言学习趣事_关于C语言中的输入输出流_续一
淘米网,启明投资淘米网两年获益近9倍
发展对象个人总结,Command 对象查询总结
弱势群体,曹参为何成为“弱势群体“?
ORACLE SQL*PLUS 命令大全
微软街景,微软允许德国民众移除街景地图中房屋照
旁氏骗局,Groupon是不是一场旁氏骗局?
扎克伯格给毕业生的话:成功没有捷径
到底什么时候,框架到底应该用什么时候用?
Android从SDCard中取得图片并设置为桌面背景
Firefox与IE在Javascript编写上的区别
个人ASP.NET程序性能优化心得(2):asp.net代码优化
淘米网股价,淘米网上市首日早盘股价下跌约16%
幕后女英雄,Twitter获苹果支持幕后英雄曝光
google平台,传Google购广告优化平台AdMeld 价格4亿美元
web框架,分享20个非常有用的Web开发工具和框架
缩小对话框,小D学blend-----如何制作对话框按钮(缩小、扩大、关闭)
Terminating a Process
侵犯专利权,一家卢森堡公司起诉Skype侵犯两项专利
趋势科技防毒墙,女CEO的20年传奇防毒经历--趋势科技陈怡桦
写给党员的一封信,写给即将入行的程序员的一封信
Jquery插件--easyUI属性汇总
ancestry,美国家谱网Ancestry市值达17亿美元
挥笔速写的意思,低调回归自然,挥笔书写代码
代码民工,探寻代码民工的根本原因
启明星,启明星Portal企业内部网站V4.3版 附演示地址 http://demo.dotnetcms.org
工欲善必先利其器,PHP学习之路——工欲善其事,必先利其器
wpfmvvm,Windows Phone 7 MVVM模式通讯方式之实现Binding Data
vs2008,初学VC++编程IDE用VS2008(个人想法)
热门标注
二叉链表
(1)
opencl驱动
(1)
三支一扶面试题
(1)
西班牙手机运营商
(1)
链表二叉树
(1)
opencl
(1)
十年一剑倒霉蛋
(1)
倒霉蛋的爱情故事
(1)
天使是个倒霉蛋
(1)
临终病人
(1)
倒霉蛋
(1)
癌症病人临终症状
(1)
临终病人的护理
(1)
智能交通上市公司
(1)
临终前会后悔的事
(1)
智能电表上市公司
(1)
点心智能操作系统
(1)
网游之倒霉蛋
(1)
宏基智能手机
(1)
临终病人最后悔
(1)
最近更新
海量数据算法,php 大数据量及海量数据处理算法总结
php数组键名,php在多维数组中根据键名快速查询其父键以及父键值的代码
php新手入门,PHP新手入门学习方法
文件名乱码,php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
冒泡排序代码,php 随机排序广告的实现代码
谷歌音乐搜索,谷歌音乐搜索栏的提示功能php修正代码
php冒泡排序,php 冒泡排序 交换排序法
php数组排序,php 归并排序 数组交集
php数组个数,php 数组的一个悲剧?
PHP读取ACCESS数据到MYSQL的代码
手机格式化代码,PHP与C#分别格式化文件大小的代码
计算器代码,PHP 计算代码执行耗时的代码修正网上普遍错误
pdo事务回滚,php中在PDO中使用事务(Transaction)
centos 5.6 升级php到5.3的方法
递归删除文件夹,rrmdir php中递归删除目录及目录下的文件
Views rows style模板重写代码
库存管理系统,php小型企业库存管理系统的设计与实现代码
java实例代码,让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
php数组函数,php简单对象与数组的转换函数代码(php多层数组和对象的转换)
php变量赋值,php的list()的一步操作给一组变量进行赋值的使用
最新标注
二叉链表
(1)
opencl驱动
(1)
三支一扶面试题
(1)
西班牙手机运营商
(1)
链表二叉树
(1)
opencl
(1)
十年一剑倒霉蛋
(1)
倒霉蛋的爱情故事
(1)
天使是个倒霉蛋
(1)
临终病人
(1)
倒霉蛋
(1)
癌症病人临终症状
(1)
临终病人的护理
(1)
智能交通上市公司
(1)
临终前会后悔的事
(1)
智能电表上市公司
(1)
点心智能操作系统
(1)
网游之倒霉蛋
(1)
宏基智能手机
(1)
临终病人最后悔
(1)
最新评论