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

discuz table_common_syscache类的fetch_all()方法代码注释

0
<?php
class table_common_syscache extends discuz_table
{
  private $_isfilecache;
  public function __construct() {
    $this->_table = 'common_syscache';
    $this->_pk = 'cname';
    $this->_pre_cache_key = '';
    $this->_isfilecache = getglobal('config/cache/type') == 'file';
    $this->_allowmem = memory('check');
    parent::__construct();
  }
  //参数$cachenames = Array(announcements, onlinelist, forumlinks, heats, historyposts, onlinerecord, userstats, diytemplatenameforum, plugin, pluginlanguage_system, setting, style_default, cronnextrun)
  public function fetch_all($cachenames) {
    $data = array();
    $cachenames = is_array($cachenames) ? $cachenames : array($cachenames);
    if ($this->_allowmem) {
      //转到memory函数上,实则调用的是discuz_memory类的get()方法
      //C::memory()->get(array(announcements, onlinelist, forumlinks, heats, historyposts, onlinerecord, userstats, diytemplatenameforum, plugin, pluginlanguage_system, setting, style_default, cronnextrun), "")
      $data = memory('get', $cachenames);

      //array array_diff(array $array1, array $array2 [, array $...])
      //返回一个数组,该数组包括了所有在array1中但是不在任何其它参数数组中的值
      $newarray = $data !== false ? array_diff($cachenames, array_keys($data)) : $cachenames;
      //如果不存在差集则返回数据$data
      if (empty($newarray)) {
        return $data;
      } else {
        $cachenames = $newarray;
      }
    }
    //$this->_isfilecache等于false,条件不满足
    if ($this->_isfilecache) {
      $lostcaches = array();
      foreach ($cachenames as $cachename) {
        if (!@include_once(DISCUZ_ROOT.'./data/cache/cache_'.$cachename.'.php')) {
          $lostcaches[] = $cachename;
        } elseif ($this->_allowmem) {
          memory('set', $cachename, $data[$cachename]);
        }
      }
      if (!$lostcaches) {
        return $data;
      }
      $cachenames = $lostcaches;
      unset($lostcaches);
    }
    $query = DB::query('SELECT * FROM '.DB::table($this->_table).' WHERE '.DB::field('cname', $cachenames));
    while ($syscache = DB::fetch($query)) {
      $data[$syscache['cname']] = $syscache['ctype'] ? unserialize($syscache['data']) : $syscache['data'];
      $this->_allowmem && (memory('set', $syscache['cname'], $data[$syscache['cname']]));
      if ($this->_isfilecache) {
        $cachedata = '$data[\''.$syscache['cname'].'\'] = '.var_export($data[$syscache['cname']], true).";\n\n";
        if (($fp = @fopen(DISCUZ_ROOT.'./data/cache/cache_'.$syscache['cname'].'.php', 'wb'))) {
          fwrite($fp, "<?php\n//Discuz! cache file, DO NOT modify me!\n//Identify: ".md5($syscache['cname'].$cachedata.getglobal('config/security/authkey'))."\n\n$cachedata?>");
          fclose($fp);
        }
      }
    }
    foreach ($cachenames as $name) {
      if ($data[$name] === null) {
        $data[$name] = null;
        $this->_allowmem && (memory('set', $name, array()));
      }
    }
    return $data;
  }
}
?>
<?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();

  //get(array(announcements, onlinelist, forumlinks, heats, historyposts, onlinerecord, userstats, diytemplatenameforum, plugin, pluginlanguage_system, setting, style_default, cronnextrun), "")
  public function get($key, $prefix='') {
    static $getmulti = null;
    $ret = false;
    if ($this->enable) {
      if (!isset($getmulti)) {
        //bool method_exists(mixed $object, string $method_name)
        //检查memory_driver_file类的getMulti()方法是否存在于指定的$this->memory对象(memory_driver_file类的实例)中
        $getmulti = method_exists($this->memory, 'getMulti');
      }
      $this->userprefix = $prefix;
      if (is_array($key)) {
        if ($getmulti) {
          //条件不满足,未执行
          $ret = $this->memory->getMulti($this->_key($key));
          if ($ret !== false && !empty($ret)) {
            $_ret = array();
            foreach ((array)$ret as $_key => $value) {
              $_ret[$this->_trim_key($_key)] = $value;
            }
            $ret = $_ret;
          }
          //End
        } else {
          $ret = array();
          $_ret = false;
          foreach ($key as $id) {
/*
$this->memory->get(qUR9F2_announcements)
$this->memory->get(qUR9F2_onlinelist)
$this->memory->get(qUR9F2_forumlinks)
$this->memory->get(qUR9F2_heats)
$this->memory->get(qUR9F2_historyposts)
$this->memory->get(qUR9F2_onlinerecord)
$this->memory->get(qUR9F2_userstats)
$this->memory->get(qUR9F2_diytemplatenameforum)
$this->memory->get(qUR9F2_plugin)
$this->memory->get(qUR9F2_pluginlanguage_system)
$this->memory->get(qUR9F2_setting)
$this->memory->get(qUR9F2_style_default)
$this->memory->get(qUR9F2_cronnextrun)
转到memory_driver_file类的get()方法上
*/
            if (($_ret = $this->memory->get($this->_key($id))) !== false && isset($_ret)) {
              $ret[$id] = $_ret;
            }
          }
        }
        if (empty($ret)) $ret = false;
      } else {
        $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;
  }
}
?>
<?php
class memory_driver_file
{
  public $cacheName = 'File';
  public $enable;
  public $path;

  public function env() {return true;}

  public function init($config) {
    //具体设置了$this->path等于"data/cache/filecache/"
    //$this->enable等于true
  }

  private function cachefile($key) {
    return str_replace('_', '/', $key).'/'.$key;
  }

  public function get($key) {
    //公告 DISCUZ_ROOT.data/cache/filecache/qUR9F2/announcements/qUR9F2_announcements.php
    //在线用户列表 DISCUZ_ROOT.data/cache/filecache/qUR9F2/onlinelist/qUR9F2_onlinelist.php
    //友情链接 DISCUZ_ROOT.data/cache/filecache/qUR9F2/forumlinks/qUR9F2_forumlinks.php
    //全论坛的热点主题 DISCUZ_ROOT.data/cache/filecache/qUR9F2/heats/qUR9F2_heats.php

    $file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
    //bool file_exists(string $filename)检查文件或目录是否存在
    //如果由filename指定的文件或目录存在则返回TRUE,否则返回FALSE
    if (!file_exists($file)) {
      return false;
    }
    $data = null;
    @include $file;
    if ($data !== null) {
      //过期的缓存数据返回false,否则返回缓存数据
      //如果设置$data['exp']等于0就直接返回缓存数据
      if ($data['exp'] && $data['exp'] < TIMESTAMP) {
        return false;
      } else {
        return $data['data'];
      }
    } else {
      return false;
    }
  }
}
?>

建站咨询

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

咨询热线

137 1731 25507×24小时服务热线

微信交流

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