时间:2021-07-01 10:21:17 帮助过:2人阅读
/*******************************************
* 文件名: /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()
{}
}
以下是对封装类的使用方法
//初始化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);
}
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了 PHP-Mmecache操作类详细介绍,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。