Discuz X memcached机制分析
看看X的memcached是如何加载和运行的。
$discuz->init(); //实例调用
Class_core.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function init() { if(!$this->initated) { $this->_init_db(); $this->_init_memory(); //今天我们关注这个,内存方法。 $this->_init_user(); $this->_init_session(); $this->_init_setting(); $this->_init_cron(); $this->_init_misc(); } $this->initated = true; } |
1 2 3 4 5 6 7 |
function _init_memory() { $this->mem = new discuz_memory(); //新建discuz_memory实例 if($this->init_memory) { $this->mem->init($this->config['memory']); //注意,init是discuz_memory的方法哦 } $this->var['memory'] = $this->mem->type; } |
这里看看discuz_memory类是怎么回事
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//构造函数 function discuz_memory() { $this->extension['eaccelerator'] = function_exists('eaccelerator_get'); $this->extension['xcache'] = function_exists('xcache_get'); $this->extension['memcache'] = extension_loaded('memcache'); //检查memcache扩展是否加载,返回bool } //_init_memory() 中,实例也调用了init() function init($config) { $this->config = $config; //这里的$config能追溯到discuz_core的构造函数,就是把config_global.php的数据加载进来 $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix']; $this->keys = array(); // memcache 接口,判断是否有mem扩展,并且config中是否设置了服务器地址 if($this->extension['memcache'] && !empty($config['memcache']['server'])) { require_once libfile('class/memcache'); $this->memory = new discuz_memcache(); $this->memory->init($this->config['memcache']); //这里的init()是discuz_memcache的方法哦,要和之前的区分。 if(!$this->memory->enable) { //检测是否能连接上memcached服务 $this->memory = null; } } |
Discuz_memcache类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class phpig_memcache { var $enable; var $obj; function phpig_memcache() { } //连接memcached服务,返回bool function init($config) { if(!empty($config['server'])) { $this->obj = new Memcache; if($config['pconnect']) { $connect = @$this->obj->pconnect($config['server'], $config['port']); } else { $connect = @$this->obj->connect($config['server'], $config['port']); } $this->enable = $connect ? true : false; } } |
对此,memcached已经分析完毕了。具体对于怎么使用它的,可以联系X的缓存机制一起阅读
http://www.oicto.com/discuz-x2-memcache-cache/