开源CMS \ discuz \ 【discuzX2】/source/class/class_core.php文件中核心高效缓存类discuz_memory分析

【discuzX2】/source/class/class_core.php文件中核心高效缓存类discuz_memory分析

总点击71
简介:<?php /** *Discuz内存读写引擎 *支持memcache,eAccelerator,XCache * *使用的时候建议直接利用函数memory()

<?php

/**

* Discuz 内存读写引擎

* 支持 memcache,eAccelerator,XCache

*

* 使用的时候建议直接利用函数 memory()

*/

class discuz_memory

{

var $config;

var $extension = array();

var $memory;

var $prefix;

var $type;

var $keys;

var $enable = false;

/**

* 确认当前系统支持的内存读写接口

* @return discuz_memory

*/

function discuz_memory() {//支持多种类型的缓存

$this->extension['eaccelerator'] = function_exists('eaccelerator_get');

$this->extension['apc'] = function_exists('apc_fetch');

$this->extension['xcache'] = function_exists('xcache_get');

$this->extension['memcache'] = extension_loaded('memcache');

}

/**

* 依据config当中设置,初始化内存引擎

* @param unknown_type $config

*/

function init($config) {

$this->config = $config;

$this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']),6).'_' : $config['prefix'];

$this->keys = array();

if($this->extension['memcache'] && !empty($config['memcache']['server'])) {

require_once libfile('class/memcache');

$this->memory = new discuz_memcache();

$this->memory->init($this->config['memcache']);

if(!$this->memory->enable) {

$this->memory = null;

}

}

if(!is_object($this->memory) && $this->extension['eaccelerator'] && $this->config['eaccelerator']) {

require_once libfile('class/eaccelerator');

$this->memory = new discuz_eaccelerator();

$this->memory->init(null);

}

if(!is_object($this->memory) && $this->extension['xcache'] && $this->config['xcache']) {

require_once libfile('class/xcache');

$this->memory = new discuz_xcache();

$this->memory->init(null);

}

if(!is_object($this->memory) && $this->extension['apc'] && $this->config['apc']) {

require_once libfile('class/apc');

$this->memory = new discuz_apc();

$this->memory->init(null);

}

if(is_object($this->memory)) {

$this->enable = true;

$this->type = str_replace('discuz_','',get_class($this->memory));

$this->keys = $this->get('memory_system_keys');

$this->keys = !is_array($this->keys) ? array() : $this->keys;

}

}

/**

* 读取内存

*

* @param string $key

* @return mix

*/

function get($key) {

$ret = null;

if($this->enable) {

$ret = $this->memory->get($this->_key($key));

if(!is_array($ret)) {

$ret = null;

if(array_key_exists($key,$this->keys)) {

unset($this->keys[$key]);

$this->memory->set($this->_key('memory_system_keys'),array($this->keys));

}

} else {

return $ret[0];

}

}

return $ret;

}

/**

* 写入内存

*

* @param string $key

* @param array_string_number $value

* @param int过期时间 $ttl

* @return boolean

*/

function set($key,$value,$ttl = 0) {

$ret = null;

if($this->enable) {

$ret = $this->memory->set($this->_key($key),array($value),$ttl);

if($ret) {

$this->keys[$key] = true;

$this->memory->set($this->_key('memory_system_keys'),array($this->keys));

}

}

return $ret;

}

/**

* 删除一个内存单元

* @param 键值string $key

* @return boolean

*/

function rm($key) {

$ret = null;

if($this->enable) {

$ret = $this->memory->rm($this->_key($key));

unset($this->keys[$key]);

$this->memory->set($this->_key('memory_system_keys'),array($this->keys));

}

return $ret;

}

/**

* 清除当前使用的所有内存

*/

function clear() {

if($this->enable && is_array($this->keys)) {

if(method_exists($this->memory,'clear')) {

$this->memory->clear();

} else {

$this->keys['memory_system_keys'] = true;

foreach ($this->keys as $k => $v) {

$this->memory->rm($this->_key($k));

}

}

}

$this->keys = array();

return true;

}

/**

* 内部函数 追加键值前缀

* @param string $str

* @return boolean

*/

function _key($str) {

return ($this->prefix).$str;

}

}

?>


意见反馈 常见问题 官方微信 返回顶部