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

discuzX3.3 savecache()函数调用时涉及执行的所有代码

0
<?php
//cache_heats.php
savecache("heats", $data);

//function_core.php
function savecache($cachename, $data) {
  C::t("common_syscache")->insert($cachename, $data);
}

//文件路径:/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",判断是否为文件缓存机制
    $this->_isfilecache = getglobal("config/cache/type") == "file";
    //检查内存功能是否可用
    $this->_allowmem = memory("check");
    //执行父类discuz_table的构造函数
    parent::__construct();
  }
  public function insert($cachename, $data) {
    //执行父类discuz_table的insert()方法
    parent::insert(array(
      "cname"=>$cachename,//"heats"
      "ctype"=>is_array($data) ? 1 : 0,
      "dateline"=>TIMESTAMP,
      "data"=>is_array($data) ? serialize($data) : $data,
    ), false, true);
    //$this->_allowmem等于File
    if ($this->_allowmem && memory("get", $cachename) !== false) {
      memory("set", $cachename, $data);
      //memory("set", "heats", $data)
    }
    $this->_isfilecache && @unlink(DISCUZ_ROOT."./data/cache/cache_".$cachename.".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类的构造函数为空
    }
    public function insert($data, $return_insert_id=false, $replace=false, $silent=false) {
      return DB::insert($this->_table, $data, $return_insert_id, $replace, $silent);
      //DB::insert(
        //"common_syscache",将数据插入pre_common_syscache数据表
        //array("cname"=>"heats","ctype"=>1,"dateline"=>TIMESTAMP,"data"=>serialize($data)),
        //false,不返回insert_id
        //true执行替换操作
      //);
    }
}

//function_core.php
//memory("get", "heats")
function memory($cmd, $key="", $value="", $ttl=0, $prefix="") {
  if ($cmd == "check") {
    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);//C::memory()->set("heats", $data, 0, "")
        break;
      case "get":
        return C::memory()->get($key, $value);//C::memory()->get("heats", "")
        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;
}

//文件路径:/source/class/discuz/discuz_memory.php
//discuz_memory类的部分代码
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() {}
  //初始化
  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) {
      $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);
        //memory_driver_file
        //$this->memory = new memory_driver_file()
        //$this->memory->init(array(["server"]=>"data/cache/filecache"))
        if (!$this->memory->enable) {
          $this->memory = null;
        } else {
          $this->type = $this->memory->cacheName;//File
          $this->enable = true;
        }
      }
    }
  }
  //C::memory()->get("heats", "")
  public function get($key, $prefix="") {
    static $getmulti = null;
    $ret = false;
    if ($this->enable) {
      //memory_driver_file类不存在getMulti方法(查看文件/source/class/memory/memory_driver_file.php)
      if (!isset($getmulti)) $getmulti = method_exists($this->memory, "getMulti");
      $this->userprefix = $prefix;
      if (is_array($key)) {
        if ($getmulti) {
          //省略部分代码
        } else {
          $ret = array();
          $_ret = false;
          foreach ($key as $id) {
            if (($_ret = $this->memory->get($this->_key($id))) !== false && isset($_ret)) {
              $ret[$id] = $_ret;
            }
          }
        }
        if (empty($ret)) $ret = false;
      } else {
        //$this->_key("heats")返回qUR9F2_heats
        //再执行$this->memory->get("qUR9F2_heats"),转到memory_driver_file类的get()方法
        $ret = $this->memory->get($this->_key($key));
        if (!isset($ret)) $ret = false;
      }
    }
    return $ret;
  }
  private function _key($str) {
    $perfix = $this->prefix.$this->userprefix;
    if (is_array($str)) {
      foreach ($str as &$val) {
        $val = $perfix.$val;
      }
    } else {
      $str = $perfix.$str;
    }
    return $str;
  }
  //C::memory()->set("heats", $data, 0, "")
  public function set($key, $value, $ttl=0, $prefix="") {
    $ret = false;
    if ($value === false) $value = "";
    if ($this->enable) {
      $this->userprefix = $prefix;
      //$this->memory->set("qUR9F2_heats", $data, 0)
      $ret = $this->memory->set($this->_key($key), $value, $ttl);
    }
    return $ret;
  }
}

class memory_driver_file {
  public $cacheName = "File";
  public $enable;
  public $path;
  private function cachefile($key) {
    return str_replace("_", "/", $key)."/".$key;
  }
  public function get($key) {
    //DISCUZ_ROOT = D:\phpStudy\WWW\
    //$this->path等于data/cache/filecache/

    //$this->cachefile("qUR9F2_heats")返回qUR9F2/heats/qUR9F2_heats
    //$file = D:\phpStudy\WWW\data/cache/filecache/qUR9F2/heats/qUR9F2_heats.php
    $file = DISCUZ_ROOT.$this->path.$this->cachefile($key).".php";
    if (!file_exists($file)) {
      return false;
    }
    $data = null;
    @include $file;
    if ($data !== null) {
      if ($data["exp"] && $data["exp"] < TIMESTAMP) {
        return false;
      } else {
        return $data["data"];
      }
    } else {
      return false;
    }
  }
  //$this->memory->set("qUR9F2_heats", $data, 0)
  public function set($key, $value, $ttl=0) {
    //$this->cachefile("qUR9F2_heats")返回qUR9F2/heats/qUR9F2_heats
    //$file = D:\phpStudy\WWW\data/cache/filecache/qUR9F2/heats/qUR9F2_heats.php
    $file = DISCUZ_ROOT.$this->path.$this->cachefile($key).".php";
    dmkdir(dirname($file));
    $data = array(
      "exp" => $ttl ? TIMESTAMP + $ttl : 0,
      "data" => $value,
    );
    file_put_contents($file, "<?php\n\$data = ".var_export($data, 1).";\n");
    return true;
  }
}
?>

建站咨询

在线咨询真诚为您提供专业解答服务

咨询热线

137 1731 25507×24小时服务热线

微信交流

二维码终于等到你,还好我没放弃
返回顶部