返回首页 | 设为主页 | 加入收藏 | 公司招聘 | 文档下载 | 网站地图
自有平台
南京普谷网络为你提供基于PHP+MYSQL的企业网站建设、电子商务网站、大型WEB项目开发、DEDECMS、PHPCMS、PHP168、UCHOME、DISCUZ等主流开源系统二次开发!
DiscuzX 1.5缓存系统流程之discuz_memory内基本介绍
DiscuzX 1.5缓存系统流程之discuz_memory内基本介绍
发布者:admin   发布时间: 2010-10-01 17:02 浏览次数:

DiscuzX 1.5缓存系统流程之discuz_memory内基本介绍

 class discuz_memory
{
 var $config;
 var $extension = array();
 var $memory;
 var $prefix;
 var $type;
 var $keys;
 var $enable = false;

 function discuz_memory() {
     //初始化三种内存缓存,确定是否支持
  $this->extension['eaccelerator'] = function_exists('eaccelerator_get');
  $this->extension['xcache'] = function_exists('xcache_get');
  $this->extension['memcache'] = extension_loaded('memcache');
 }

 function init($config) {

  $this->config = $config;
  //缓存前缀
  $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];
  //缓存变量
  $this->keys = array();
       
  //如果MEMCACHE缓存模式支持且配置中定义了主机地址,则加载memcache缓存模式
  if($this->extension['memcache'] && !empty($config['memcache']['server'])) {
   //加载memcache缓存操作类
      require_once libfile('class/memcache');
   //初始化类
   $this->memory = new discuz_memcache();
   //初始化MEMCACHE缓存系统
   $this->memory->init($this->config['memcache']);
   //如果初始化失败,则销毁$this->memory,销毁后继续寻找其他缓存方式
   if(!$this->memory->enable) {
    $this->memory = null;
   }
  }
        //如果 1、memcache缓存加载无效 2、环境支持eaccelerator加速,$_config启用了eaccelerator加速,则开始使用eaccelerator加速方式
  if(!is_object($this->memory) && $this->extension['eaccelerator'] && $this->config['eaccelerator']) {
   require_once libfile('class/eaccelerator');
   //加载eaccelerator缓存操作类
   $this->memory = new discuz_eaccelerator();
   //初始化eaccelerator缓存系统,注:无需任何参数
   $this->memory->init(null);
  }
        //如果 1、memcache、eaccelerator缓存加载无效 2、环境支持xcache加速,$_config启用了xcache加速,则开始使用xcache加速方式
  if(!is_object($this->memory) && $this->extension['xcache'] && $this->config['xcache']) {
   //加载xcache缓存操作类
      require_once libfile('class/xcache');
   $this->memory = new discuz_xcache();
   //初始化xcache缓存系统,注:无需任何参数
   $this->memory->init(null);
  }
        //如果xcache、eaccelerator、memcache任何一种加速方式成功启用
  if(is_object($this->memory)) {
      //启用缓存优化
   $this->enable = true;
   //get_class:获取一个对象的类名,$this->type返回缓存模式:xcache、eaccelerator、memcache
   $this->type = str_replace('discuz_', '', get_class($this->memory));
   //获取memory_system_keys的缓存值
   //此类的get\set\clear是属于工厂模式,自动调用相应的缓存优化类的get/set/rm方法
   $this->keys = $this->get('memory_system_keys');
   //转为数组
   $this->keys = !is_array($this->keys) ? array() : $this->keys;
  }

 }

 function get($key) {
  $ret = null;
  //如果启用了缓存
  if($this->enable) {
      //ret为读取的
   $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;
 }

 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;
 }

 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)) {
   $this->keys['memory_system_keys'] = true;
   foreach ($this->keys as $k => $v) {
    $this->memory->rm($this->_key($k));
   }
  }
  $this->keys = array();
  return true;
 }

 function _key($str) {
  return ($this->prefix).$str;
 }

}

更详细介绍请参考:DiscuzX 1.5缓存系统流程

Copyright © 2010-2012 www.leaphp.com, All Rights Reserved
地址:中国· 南京 白下路111号洪宇商务中心8310室
苏ICP备08012533号