您当前的位置:首页 > 网站建设笔记 >

discuzX3.3 loadcache()函数

0
<?php
//$cachenames = Array(announcements, onlinelist, forumlinks, heats, historyposts, onlinerecord, userstats, diytemplatenameforum, plugin, pluginlanguage_system, setting, style_default, cronnextrun)

//table_forum_post.php
//public static function getposttablebytid($tids, $primary=0) {...}方法里loadcache('threadtableids');
function loadcache($cachenames, $force=false) {
    global $_G;
    static $loadedcache = array();
    $cachenames = is_array($cachenames) ? $cachenames : array($cachenames);
    $caches = array();
    foreach ($cachenames as $k) {
        if (!isset($loadedcache[$k]) || $force) {
            $caches[] = $k;
            $loadedcache[$k] = true;
        }
    }
    if (!empty($caches)) {
        //自动加载/source/class/table/table_common_syscache.php类文件
        //实例化table_common_syscache类的一个对象,该对象存储在类C的$_tables静态属性,具体在$_tables["table_common_syscache"]

        //在实例化table_common_syscache类的时候,自动首先执行了table_common_syscache类的构造函数,然后调用了discuz_table类的构造函数(table_common_syscache类继承了discuz_table类)

        //最后返回table_common_syscache类的一个对象并执行该对象的fetch_all()方法
        $cachedata = C::t("common_syscache")->fetch_all($caches);
        foreach ($cachedata as $cname => $data) {
            if ($cname == "setting")
            {
                //更新$_G["setting"],保存的数据量大
                $_G["setting"] = $data;
            }
            elseif ($cname == "usergroup_".$_G["groupid"])
            {
                $_G["cache"][$cname] = $_G["group"] = $data;
            }
            elseif ($cname == "style_default")
            {
                $_G["cache"][$cname] = $_G["style"] = $data;
            }
            elseif ($cname == "grouplevels")
            {
                $_G["grouplevels"] = $data;
            }
            else
            {
                $_G["cache"][$cname] = $data;
            }
        }
    }
    return true;
}

//文件路径:/source/class/table/table_common_syscache.php
//table_common_syscache类的部分代码
class table_common_syscache extends discuz_table {
    private $_isfilecache;

    public function __construct() {
        $this->_table = "common_syscache";
        $this->_pk = "cname";
        $this->_pre_cache_key = "";

        //比较$_G["config"]["cache"]["type"]当前的值是否等于"file",判断是否为文件缓存机制
        //打印getglobal("config/cache/type")或者$_G["config"]["cache"]["type"]等于NULL
        $this->_isfilecache = getglobal("config/cache/type") == "file";
        //转到memory函数上,检查内存功能是否可用
        $this->_allowmem = memory("check");
        parent::__construct();
    }
}
function memory($cmd, $key="", $value="", $ttl=0, $prefix="") {
  if ($cmd == "check") {
    //转到class_core类的memory()方法上
    return C::memory()->enable ? C::memory()->type : "";
  } elseif (C::memory()->enable && in_array($cmd, array("set","get","rm","inc","dec"))) {
    //省略部分代码if (defined("DISCUZ_DEBUG") && DISCUZ_DEBUG) {......}
    switch ($cmd) {
      case "set":
        return C::memory()->set($key, $value, $ttl, $prefix);
        break;
      case "get":
        return C::memory()->get($key, $value);
        break;
      case "rm":
        return C::memory()->rm($key, $value);
        break;
      case "inc":
        return C::memory()->inc($key, $value ? $value : 1);
        break;
      case "dec":
        return C::memory()->dec($key, $value ? $value : -1);
        break;
    }
  }
  return null;
}
public static function memory() {
	if (!self::$_memory) {
		//转到实例化discuz_memory类的构造函数和init()方法上
		self::$_memory = new discuz_memory();
		self::$_memory->init(self::app()->config['memory']);
	}
	return self::$_memory;
}
<?php
class discuz_memory extends discuz_base
{
    private $config;
    private $extension = array();
    private $memory;
    private $prefix;
    private $userprefix;
    public $type;
    public $enable = false;
    public $debug = array();

    public function __construct() {}

//打印self::app()->config['memory']的值
self::app()->config['memory'] = array(
    'prefix'=>'qUR9F2_',
    'redis'=>array(
        'server'=>'',
        'port'=>6379,
        'pconnect'=>1,
        'timeout'=>'0',
        'requirepass'=>'',
        'serializer'=>1,
    ),
    'memcache'=>array(
        'server'=>'',
        'port'=>11211,
        'pconnect'=>1,
        'timeout'=>1,
    ),
    'apc'=>'0',
    'apcu'=>'0',
    'xcache'=>'0',
    'eaccelerator'=>'0',
    'wincache'=>'0',
    'yac'=>'0',
    'file'=>array('server'=>'data/cache/filecache',),
);

    public function init($config) {
        $this->config = $config;
        $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];
        unset($this->config['prefix']);
        foreach ($this->config as $cache => $config) {
            //直到循环: is_array(array('server'=>'data/cache/filecache'))为真
            //$available等于true
            $available = is_array($config) ? !empty($config['server']) : !empty($config);
            if ($available && !is_object($this->memory))
            {
                $class_name = 'memory_driver_'.$cache;
                $this->memory = new $class_name();
                $this->memory->init($config);
                //$this->memory = new memory_driver_file()
                //$this->memory->init(array('server'=>'data/cache/filecache'))
                //转到实例化memory_driver_file类的构造函数和init()公共方法上,待执行完后,此时discuz_memory类的私有属性$this->memory存储的是memory_driver_file类的一个实例,可以从$this->memory对象(memory_driver_file类的一个实例)访问到的公共属性有$this->memory->enable等于true、$this->memory->cacheName等于"File"、$this->memory->path等于"data/cache/filecache/"
                if (!$this->memory->enable) {
                    $this->memory = null;
                } else {
                    //discuz_memory类的实例公共属性$this->type等于"File",也就是C::memory()->type等于"File"
                    //discuz_memory类的实例公共属性$this->enable等于true,也就是C::memory()->enable等于true
                    $this->type = $this->memory->cacheName;
                    $this->enable = true;
                }
            }
        }
    }
}
?>
<?php
class memory_driver_file
{
    public $cacheName = 'File';
    public $enable;
    public $path;

    public function env() {return true;}

    //init(array('server'=>'data/cache/filecache'))创建缓存目录
    public function init($config) {
        $this->path = $config['server'].'/';
        if ($config['server']) {
            //DISCUZ_ROOT = D:\phpStudy\WWW\pcool\
            //bool is_dir(string $filename)
            //判断给定文件名是否是一个目录
            //filename如果文件名存在并且为目录则返回true。如果filename是一个相对路径,则按照当前工作目录检查其相对路径。
            //如果文件名存在,并且是个目录,返回TRUE,否则返回FALSE。
            $this->enable = is_dir(DISCUZ_ROOT.$this->path);
            //$this->enable = is_dir(D:\phpStudy\WWW\pcool\data/cache/filecache/)
            if (!$this->enable) {
                //http://www.wangzhanchengxu.com/wangzhanbj/222.html
                dmkdir(DISCUZ_ROOT.$this->path);
                $this->enable = is_dir(DISCUZ_ROOT.$this->path);
            }
        } else {
            $this->enable = false;
        }
    }
}
?>
<?php
//文件路径:/source/class/discuz/discuz_table.php
//discuz_table类的部分代码
class discuz_table extends discuz_base {
    //公共属性
    public $data = array();
    public $methods = array();
    //受保护的属性
    protected $_table;
    protected $_pk;
    protected $_pre_cache_key;
    protected $_cache_ttl;
    protected $_allowmem;
    //构造函数
    public function __construct($para=array()) {
        if (!empty($para)) {
            $this->_table = $para["table"];
            $this->_pk = $para["pk"];
        }
        if (isset($this->_pre_cache_key) &&
            (($ttl=getglobal("setting/memory".$this->_table)) !== null || ($ttl=$this->_cache_ttl) !== null) &&
            memory("check")
            ) {
            $this->_cache_ttl = $ttl;
            $this->_allowmem = true;
        }
        $this->_init_extend();//方法定义为空
        parent::__construct();//discuz_base类的构造函数为空
    }
}
?>

下一步转向table_common_syscache类的fetch_all()方法

建站咨询

咨询热线

微信交流

返回顶部