【php】缓存类的编写:支持文件缓存和memcache缓存
2012-08-27 18:23
661 查看
<?php //与上一篇文章:使用mysqli完成的一个数据库操作类 搭配使用 abstract class Abstract_Cache { protected $cache_time; public function set_cache_time($cache_time) { $this->cache_time = $cache_time;//设置缓存时间,可写在公共的配置文件中 } abstract function &get($key); abstract function set($key, $data); abstract function add($key, $data); abstract function del($key); abstract function get_key($key); abstract function flush(); } class cache_file extends Abstract_Cache { private $cache_dir; public function set_cache_dir($cache_dir = './cache/') { $this->cache_dir = $cache_dir; } public function mkpath($dir) { //层级的创建目录 return is_dir($dir) or ($this->mkpath(dirname($dir)) and (mkdir($dir, 0777) and chmod($dir,0777))); } public function &get($key, $cache_time = '') { if(empty ($cache_time)) { $cache_time = $this->cache_time; } else { $cache_time = intval($cache_time); } $cache_encode_key = $this->get_key($key); //缓存文件的名称 $filename = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'.$cache_encode_key.".cache.php"; //缓存文件是否是否存在和过期 if(!is_file($filename) || time() - filemtime($filename) > $cache_time) { return false; } else { return unserialize(file_get_contents($filename));//文件中缓存的数据为序列化格式的数据,所以取出时需要将其反序列化 } } public function set($key,$data) { $cache_encode_key = $this->get_key($key); $cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'; $filename = $cache_dir.$cache_encode_key.".cache.php"; if( $this->mkpath($cache_dir) ) { $out = serialize($data); file_put_contents($filename, $out); } } public function add($key, $data) { $cache_encode_key = $this->get_key($key); $cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'; $filename = $cache_dir.$cache_encode_key.".cache.php"; if(is_file($filename)) { return false; } else { $this->set($key, $data); } } public function del($key) { $cache_encode_key = $this->get_key($key); $cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'; $filename = $cache_dir.$cache_encode_key.".cache.php"; if(is_file($filename)) { return false; } else { @unlink($filename); } } public function flush() { $this->remove_dir($this->cache_dir,false); } public function get_key($key){ Return md5(CACHE_KEY.$key); } private function remove_file($dir) { if (is_dir($dir)) { $dh = opendir($dir); while (false !== ($file = readdir($dh))) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; if(!is_dir($fullpath)) { @unlink($fullpath); } else { $this->remove_dir($fullpath); } } } closedir($dh); } } private function remove_dir($dir, $self = true) { $this->remove_file($dir); if ($self == true && is_dir($dir)) { rmdir($dir); } } } class cache_memcache extends Abstract_Cache { var $_memcache = null; /** * 连接到缓存服务器 * * @author * @param array $options * @return bool */ function __construct($memcache_conf) { $this->_memcache = new Memcache; return $this->_memcache->connect($memcache_conf['host'], $memcache_conf['port']); } /** * 写入缓存 * * @author * @param none * @return void */ function set($key, $value, $ttl = null) { $cache_encode_key = $this->get_key($key); return $this->_memcache->set($cache_encode_key, $value, $ttl); } /** * 获取缓存 * * @author * @param string $key * @return mixed */ function &get($key) { $cache_encode_key = $this->get_key($key); return $this->_memcache->get($cache_encode_key); } /** * 写入缓存 * * @author * @param none * @return void */ function add($key, $value, $ttl = null) { $cache_encode_key = $this->get_key($key); return $this->_memcache->add($cache_encode_key, $value, $ttl); } /** * 清空缓存 * * @author * @return bool */ function flush() { return $this->_memcache->flush(); } function del($key) { $cache_encode_key = $this->get_key($key); return $this->_memcache->delete($key); } public function get_key($key) { $cache_encode_key = $this->get_key($key); Return md5(CACHE_KEY.$key); } }
相关文章推荐
- php 写入缓存文件、读取缓存文件的函数代码
- 【PHP】缓存技术!缓存类,看看原理吧
- php学习笔记(十一)文件上传类的编写
- linux下的nginx中php文件出现缓存的问题
- PHP之编写日志文件留后门(免杀)
- php实现的支持断点续传的文件下载类
- PHP文件缓存类示例分享
- 用PHP读写音频文件的信息(支持WMA和MP3)
- php支持分块与断点续传文件下载功能代码
- Linux Nginx安装配置文件支持PHP
- 解析用PHP读写音频文件信息的详解(支持WMA和MP3)
- php文件缓存类
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- PHP 下载远程文件类(支持断点续传)
- php文件上传类,支持产生文件缩略图
- php生成zip压缩文件的方法,支持文件和压缩包路径查找
- xp下iis支持aspx和php文件
- php 支持断点续传的文件下载类
- php文件缓存类用法实例分析