0

0

PHP Memcached + APC + 文件缓存封装实现代码

php中文网

php中文网

发布时间:2016-06-13 12:18:40

|

1171人浏览过

|

来源于php中文网

原创

使用方法:
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

复制代码 代码如下:



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);
}

}
?>

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

nginx部署php项目教程汇总
nginx部署php项目教程汇总

本专题整合了nginx部署php项目教程汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 8.6万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号