PHP-Mmecache操作类详细介绍
来源:互联网 发布:剑三花姐捏脸数据截图 编辑:程序博客网 时间:2024/06/10 04:56
下面类对memcache进行封装,包括了对memcache的添加,读取,清空,删除,获取服务器的信息,缓存服务池等。
<?php/******************************************* * 文件名: /includes/memcache.class.php * 功能: memcache 缓存类 * 版本: 1.0 * 日期: 2016-07-16 * 程序名: memcache缓存操作类 -----(PHP中需加载memcache扩展) * 作者: JoeXiong * 版权: Copyright@2016-2016 github.com/JoeXiong All Rights Reserved *********************************************/class joememcache{ private static $_instance; private $_memcache; private $_which = 0; private $_memservers; public static function getInstance() { if (! (self::$_instance instanceof joememcache)) { self::$_instance = new self(); } return is_object(self::$_instance->_memcache) ? self::$_instance : false; } private function __construct() { if (extension_loaded('memcache')) { $this->_memcache = new Memcache(); $this->_which = 1; } elseif (extension_loaded('memcached')) { $this->_memcache = new Memcached(); $this->_which = 2; } else $this->_memcache = FALSE; } /** * 保存缓存 * @param unknown $key * @param unknown $data * @param number $ttl * @param string $isCompress * @return boolean */ public function Save($key, $data, $ttl = 60,$isCompress = FALSE){ if($this->_which == 1) return $this->_memcache->set($key, array($data, time(), $ttl), !$isCompress ? 0 : MEMCACHE_COMPRESSED, $ttl);//使用time() 函数最新 else if($this->_which == 2) return $this->_memcache->set($key, array($data, time(), $ttl), $ttl); else return FALSE; } /** * 读取缓存信息 * @param unknown $key * @return Ambigous <multitype:, string>|boolean */ public function readMetaData($key){ $value = $this->_memcache->get($key); if(is_array($value) && count($value) == 3){ list($data, $time, $ttl) = $value; return (time() < $time + $ttl) ? $data : array(); } else { return false; } } /** * @deprecated 读取多个缓存信息 * @param array $array * @return 成功$value(array), 失败FALSE(bool) */ public function readMultiData($keys) { if(!is_array($keys)) { return FALSE; } $rtn = array(); if($this->_which == 1) $rtn = $this->_memcache->get($keys); else if($this->_which == 2) $rtn = $this->_memcache->getMulti($keys); $now = time(); foreach($rtn as $key=>&$v) { if(!empty($v)) { list($data, $time, $ttl) = $v; $v = ($now < $time + $ttl) ? $data : array(); } } return $rtn; } /** * @description 读取缓存 * @param $key 查询索引key * @return 成功 array 失败 FALSE */ public function Read($key) { $data = $this->_memcache->get($key); return is_array($data) ? $data[0] : FALSE; } /** * @description 删除缓存 * @param $key 将要删除的key * return bool 成功 TRUE 失败 FALSE */ public function Delete($key) { return $this->_memcache->delete($key); } /** * @description 清空所有缓存 * @return bool true or false */ public function Clear() { return $this->_memcache->flush(); } /** * 获取缓存服务器池中所有服务器统计信息 * @return array */ public function getExtendedStats() { //return $this->_memcache->getExtendedStats(); if($this->_which == 1) { return $this->_memcache->getExtendedStats(); } else if($this->_which == 2) return $this->_memcache->getServerList(); else return FALSE; } /** * 缓存服务器池 */ public function addServer() { foreach ($this->_memservers as $h) { $this->_memcache->addServer($h['host'], isset($h['port']) ? $h['port'] : 11211); // 默认端口为11211 } } /** * 获取memecache服务器地址 */ public function getHost() { return $this->_memservers; } /** * 设置memcache服务器地址 */ public function setHost(array $servers) { if (is_array($servers) && ! empty($servers)) { $this->_memservers = $servers; $this->addServer(); } } private function __clone() {}}
以下是对封装类的使用方法
//初始化memcache $memcache = joememcache::getInstance(); //是否开启memcache扩展 if($memcache){ //memcache的配置参数,放在配置文件中 global $memServer,$memKey; //设置memecache服务器地址 $memcache->setHost($memServer); //通过key读取数据 $news = $memcache->readMetaData($memKey['sharetypekey']['10001']['Key'].$id); //memcache没有当前key的数据,则从数据库查询并保存到memecache服务器中 if(!$news){ $news = $this->getCurSharenewsInfo($id); $memcache->Save($memKey['sharetypekey']['10001']['Key'].$id,$news,$memKey['sharetypekey']['10001']['Time']); } }else{ //直接数据库查询数据 $news = $this->getCurSharenewsInfo($id); }
0 0
- PHP-Mmecache操作类详细介绍
- php $_SERVER详细介绍
- php详细介绍
- SVN操作详细介绍
- 详细介绍PHP应用提速
- php安全模式详细介绍
- php安全模式详细介绍
- php解析xml详细介绍
- php 实现分页详细介绍
- PHP 中$_SERVER 详细介绍
- php安全模式详细介绍
- 详细的redis操作介绍
- 详细介绍 NPOI操作Excel
- PHP操作MongoDB数据库详细例子介绍(增、删、改、查) (六)
- PHP操作MongoDB打模块 linux下,详细截图介绍(七)
- snoopy(强大的PHP采集类) 详细介绍
- PHP 类的自动加载详细介绍 leyangjun
- snoopy(强大的PHP采集类) 详细介绍
- ReentrantLock与synchronized在与数据库交互时的问题
- Unix下的进程管理
- POJ 1426
- 开等问题
- 4 硬件
- PHP-Mmecache操作类详细介绍
- delphi导入导出excel|oracle
- [LeetCode] 11. Container With Most Water
- Java 中Instanceof的用法
- 杭电ACM 1008
- GitHub for Windows 安装失败,An error occurred attempting to install github 的解决办法
- 5 LARIX软件
- Xcode 安装插件
- C语言学习之位操作符的使用