PHP多类型缓存封装实现详解
使用示例如下所示。
针对Memcached后端的调用方式。
结束代码块标记。
具体实现代码如下:
首先实例化一个Memcache缓存对象:$cache = new Cache_MemCache();
添加第一个服务器节点:$cache->addServer('www1');
添加第二个服务器节点并设置权重:$cache->addServer('www2',11211,20); // 该服务器拥有双倍内存,因此分配双倍权重
添加第三个服务器节点:$cache->addServer('www3',11211);
将数据存入缓存,有效期为10分钟。
执行存储操作:$cache->store('my_key','foobar',600);
从缓存中读取数据。
输出缓存内容:echo($cache->fetch('my_key'));
针对文件后端的调用方式。
结束代码块标记。
具体实现代码如下:
实例化一个文件缓存对象:$cache = new Cache_File();
定义缓存键名:$key = 'getUsers:selectAll';
检查数据是否已存在于缓存中。
如果缓存未命中,则执行以下逻辑:if (!$data = $cache->fetch($key)) {
假设此处存在数据库连接资源。
执行SQL查询:$result = mysql_query("SELECT * FROM users");
初始化结果数组:$data = array();
遍历结果集并将数据放入数组。
循环获取每一行数据:while($row = mysql_fetch_assoc($result)) { $data[] = $row; }
将获取到的数据存储到文件中,有效期为10分钟。
执行文件存储操作:$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);
}
实现APC缓存子类:class Cache_APC extends Cache_Abstract {
实现APC的获取方法:function fetch($key) {
返回APC获取的结果:return apc_fetch($key);
}
实现APC的存储方法:function store($key, $data, $ttl) {
返回APC存储的结果:return apc_store($key, $data, $ttl);
}
实现APC的删除方法:function delete($key) {
返回APC删除的结果:return apc_delete($key);
}
}
实现Memcache缓存子类:class Cache_MemCache extends Cache_Abstract {
声明连接属性:public $connection;
构造函数初始化:function __construct() {
创建新的Memcache实例:$this->connection = new MemCache;
}
实现Memcache的存储方法:function store($key, $data, $ttl) {
调用底层set方法,参数0表示不压缩:return $this->connection->set($key, $data, 0, $ttl);
}
实现Memcache的获取方法:function fetch($key) {
调用底层get方法:return $this->connection->get($key);
}
实现Memcache的删除方法:function delete($key) {
调用底层delete方法:return $this->connection->delete($key);
}
添加服务器的主控方法:function addServer($host, $port = 11211, $weight = 10) {
调用底层addServer,第三个参数true表示永久连接:$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))
不存在则返回false:return false;
以只读模式打开文件:$h = fopen($filename, 'r');
判断文件句柄是否有效:if (!$h)
无效则返回false:return false;
获取共享锁:flock($h, LOCK_SH);
读取文件全部内容:$data = file_get_contents($filename);
关闭文件句柄:fclose($h);
反序列化数据:$data = @ unserialize($data);
判断反序列化是否成功:if (!$data) {
清理损坏的文件:unlink($filename);
返回false:return false;
}
判断当前时间是否超过过期时间:if (time() > $data[0]) {
清理过期文件:unlink($filename);
返回false:return false;
}
返回实际数据部分:return $data[1];
}
实现文件的删除方法:function delete($key) {
获取文件名路径:$filename = $this->getFileName($key);
判断文件是否存在:if (file_exists($filename)) {
存在则删除并返回结果:return unlink($filename);
}
否则进入else分支:else {
返回false:return false;
}
}
私有方法用于生成文件名:private function getFileName($key) {
返回拼接后的路径:return '/tmp/s_cache' . md5($key);
}
}
?>