PHP-Mmecache操作类详细介绍
程序员文章站
2022-04-07 11:52:31
...
下面类对memcache进行封装,包括了对memcache的添加,读取,清空,删除,获取服务器的信息,缓存服务池等。
/*******************************************
* 文件名: /includes/memcache.class.php
* 功能: memcache 缓存类
* 版本: 1.0
* 日期: 2016-07-16
* 程序名: memcache缓存操作类 -----(PHP中需加载memcache扩展)
* 作者: JoeXiong
* 版权: Copyright@2016-2016 github.com/JoeXiong All Rights Reserved
*********************************************/classjoememcache
{privatestatic$_instance;
private$_memcache;
private$_which = 0;
private$_memservers;
publicstaticfunctiongetInstance()
{if (! (self::$_instanceinstanceof joememcache)) {
self::$_instance = newself();
}
return is_object(self::$_instance->_memcache) ? self::$_instance : false;
}
privatefunction__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
*/publicfunctionSave($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() 函数最新elseif($this->_which == 2)
return$this->_memcache->set($key, array($data, time(), $ttl), $ttl);
elsereturnFALSE;
}
/**
* 读取缓存信息
* @param unknown $key
* @return Ambigous |boolean
*/ publicfunctionreadMetaData($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 {
returnfalse;
}
}
/**
* @deprecated 读取多个缓存信息
* @param array $array
* @return 成功$value(array), 失败FALSE(bool)
*/publicfunctionreadMultiData($keys)
{if(!is_array($keys)) {
returnFALSE;
}
$rtn = array();
if($this->_which == 1)
$rtn = $this->_memcache->get($keys);
elseif($this->_which == 2)
$rtn = $this->_memcache->getMulti($keys);
$now = time();
foreach($rtnas$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
*/publicfunctionRead($key)
{$data = $this->_memcache->get($key);
return is_array($data) ? $data[0] : FALSE;
}
/**
* @description 删除缓存
* @param $key 将要删除的key
* return bool 成功 TRUE 失败 FALSE
*/publicfunctionDelete($key)
{return$this->_memcache->delete($key);
}
/**
* @description 清空所有缓存
* @return bool true or false
*/publicfunctionClear()
{return$this->_memcache->flush();
}
/**
* 获取缓存服务器池中所有服务器统计信息
* @return array
*/publicfunctiongetExtendedStats()
{//return $this->_memcache->getExtendedStats();if($this->_which == 1) {
return$this->_memcache->getExtendedStats();
}
elseif($this->_which == 2)
return$this->_memcache->getServerList();
elsereturnFALSE;
}
/**
* 缓存服务器池
*/publicfunctionaddServer()
{foreach ($this->_memservers as$h) {
$this->_memcache->addServer($h['host'], isset($h['port']) ? $h['port'] : 11211); // 默认端口为11211
}
}
/**
* 获取memecache服务器地址
*/publicfunctiongetHost()
{return$this->_memservers;
}
/**
* 设置memcache服务器地址
*/publicfunctionsetHost(array $servers)
{if (is_array($servers) && ! empty($servers)) {
$this->_memservers = $servers;
$this->addServer();
}
}
privatefunction__clone()
{}
}