使用方法:
Memcached
复制代码 代码如下:
$cache = new Cache_MemCache();
$cache->addServer('www1');
$cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight
$cache->addServer('www3',11211);
// Store some data in the cache for 10 minutes
$cache->store('my_key','foobar',600);
// Get it out of the cache again
echo($cache->fetch('my_key'));
文件缓存
复制代码 代码如下:
$cache = new Cache_File();
$key = 'getUsers:selectAll';
// check if the data is not in the cache already
if (!$data = $cache->fetch($key)) {
// assuming there is a database connection
$result = mysql_query("SELECT * FROM users");
$data = array();
// fetching all the data and putting it in an array
while($row = mysql_fetch_assoc($result)) { $data[] = $row; }
// Storing the data in the cache for 10 minutes
$cache->store($key,$data,600);
}
下载: class_cache3.php
复制代码 代码如下:
<?php
abstract class Cache_Abstract {
abstract function fetch($key);
abstract function store($key, $data, $ttl);
abstract function delete($key);
}
class Cache_APC extends Cache_Abstract {
function fetch($key) {
return apc_fetch($key);
}
function store($key, $data, $ttl) {
return apc_store($key, $data, $ttl);
}
function delete($key) {
return apc_delete($key);
}
}
class Cache_MemCache extends Cache_Abstract {
public $connection;
function __construct() {
$this->connection = new MemCache;
}
function store($key, $data, $ttl) {
return $this->connection->set($key, $data, 0, $ttl);
}
function fetch($key) {
return $this->connection->get($key);
}
function delete($key) {
return $this->connection->delete($key);
}
function addServer($host, $port = 11211, $weight = 10) {
$this->connection->addServer($host, $port, true, $weight);
}
}
class Cache_File extends Cache_Abstract {
function store($key, $data, $ttl) {
$h = fopen($this->getFileName($key), 'a+');
if (!$h)
throw new Exception('Could not write to cache');
flock($h, LOCK_EX);
fseek($h, 0);
ftruncate($h, 0);
$data = serialize(array(time() + $ttl, $data));
if (fwrite($h, $data) === false) {
throw new Exception('Could not write to cache');
}
fclose($h);
}
function fetch($key) {
$filename = $this->getFileName($key);
if (!file_exists($filename))
return false;
$h = fopen($filename, 'r');
if (!$h)
return false;
flock($h, LOCK_SH);
$data = file_get_contents($filename);
fclose($h);
$data = @ unserialize($data);
if (!$data) {
unlink($filename);
return false;
}
if (time() > $data[0]) {
unlink($filename);
return false;
}
return $data[1];
}
function delete($key) {
$filename = $this->getFileName($key);
if (file_exists($filename)) {
return unlink($filename);
}
else {
return false;
}
}
private function getFileName($key) {
return '/tmp/s_cache' . md5($key);
}
}
?>
相关推荐:
SEO难吗?揭秘SEO背后的真相,让你轻松上手!,大旺百度网站推广
AI生成网页模板,轻松打造专业网站,ai网格画法
SEO指:如何通过精准优化提高网站流量和转化率,无锡论坛营销推广要多久
seo高手有什么条件,seo难上手吗 ,指南ai写作电力创新
《收录情况:数字时代的网络信息检索与价值体现》,山东全域营销推广软件客服电话
SEO站外优化策略:提升网站排名的关键因素,遵义公司网站推广
ChatGPT破解版:让人工智能助力你的工作与生活,ai cos帽
seo网络推广是什么,seo网络推广是什么意思 ,ai哪里注音
SEO快速优化技术:助力网站流量暴增,轻松登顶搜索引擎,铜陵ai全网通推广软件
ChatGPT为什么页面下拉不了?问题解析与解决方案,ai对准
什么是SEO优化方案,seo的优化方案 ,ai emorobot
ChatGPT付款银行卡被拒绝?解决方案全解析!,小发猫ai智能写作查重能过吗
seo涉及什么内容,seo主要包括 ,法医使用ai
SEO精通:让你的内容在搜索引擎中脱颖而出,跨境电商 推广营销
ChatGPT进不去怎么办?解决方案与技巧,轻松畅享智能对话,ai va
ChatGPT免费订阅的使用限制:其潜力与挑战,ai辅助线无法对齐画板
seo资源是什么意思,seo资源怎么打开 ,梅州ai自动插件加工
ChatGPT付款被拒?如何应对与解决常见支付问题,掉包ai
GPT在智能聊天机器人中的作用:重塑沟通体验,开创智能未来,百万级ai
seo要学什么技术,seo要学什么技术好 ,ai.fale
seo需要什么人才,seo需要做什么工作 ,探索ai照片
ChatGPT的VPN梯子:畅享全球互联网自由,打破地域限制,一直搜Ai
AI热门软件,未来科技的钥匙
ChatGPT:OpenAI的创新之作-一款颠覆传统的语言模型,ai球衣号
SEO流程全解析:如何提升网站流量与排名,账号如何营销推广
SEO关键词是什么意思?全面解析SEO关键字的核心作用,华为ai音箱 百度ai音箱
SEO技巧提升网站流量:打造高效网站的关键策略,Ai测脸相
SEO优化如何为网站做好关键词研究和优化,ai直通
ChatGPT页面怎么拖不动?解决问题的终极指南,日韩AI换脸在线观看
互联网快照:记录数字时代的每个瞬间,全网seo怎么优化内容
SEO广告:如何借助SEO提升品牌曝光与销售业绩?,网站推广怎么选择
文章AI思维导图自动生成助力创作的智慧之源
SOE优化:让你的网站轻松登顶,流量激增的秘密武器,广西网站建设存在问题
seo给你什么帮助,seo的利与弊 ,ai 大厨
SEO兼职:如何通过SEO兼职实现收入增长与职业突破,濮阳网站建设官网
智能AI写文章:高效创作新风尚
seo,seoul city ,ai精洗
什么是seo行业,seo是什么职业做什么的 ,ai标题字体效果
摘要AI生成:高效工作的新时代利器
文字写作AI生成工具:让创作更简单、更高效
seo重点是什么,seo最重要的指标 ,ai网格怎么用
京东业绩再创新高:未来电商巨头的成功之道,如何做seo优化留痕
为什么seo这么难,seo难嘛 ,ai宁中则
seo软件叫什么,seo软件视频教程 ,eps ai 缩略图
ChatGPT为什么用不了了?背后的真相揭秘!,ai写作专家收费吗
OpenAIGPT:开启智能时代的语言革命,ai辣妹动漫
SEO优化快:如何在短时间内实现网站流量爆发,Lacey0508ai
SEO表格:优化网站排名的秘密武器,大数据推广营销费用多少
ChatGPT198元永久会员,开启智慧之门,体验AI的极致服务!,老孙教ai
SEO特点与实施策略:提升网站流量与排名的关键,定西抖音seo价格查询