示例#1
0
 public function loadConfig()
 {
     //todo fix empty config caching
     $cache = new \CPHPCache();
     $cacheFile = $_SERVER['DOCUMENT_ROOT'] . "/bitrix/cache/" . $cache->GetPath(__CLASS__);
     // проверяем, обновлялся ли конфиг
     $cacheWritten = filemtime($cacheFile);
     $configWritten = filemtime($this->configFile);
     // устаревший кеш или неудачно начатый кеш перезаписываем
     if ($configWritten > $cacheWritten || !$cache->InitCache(self::TTL, __CLASS__, '/')) {
         $cache->Clean(__CLASS__, '/');
         try {
             parent::loadConfig();
             if ($cache->StartDataCache(self::TTL, __CLASS__, '/')) {
                 $cache->EndDataCache(array('config' => $this->config));
             } else {
                 _log('Caching failed', 'widgets');
             }
         } catch (Exception $e) {
             _log('loading config error: ' . $e->getMessage(), 'widgets');
         }
     } else {
         $vars = $cache->GetVars();
         $this->config = $vars['config'];
     }
 }
示例#2
0
 function InitCache($TTL, $uniq_str, $initdir = false, $basedir = "cache")
 {
     global $APPLICATION, $USER;
     if ($initdir === false) {
         $initdir = $APPLICATION->GetCurDir();
     }
     $this->basedir = BX_PERSONAL_ROOT . "/" . $basedir . "/";
     $this->initdir = $initdir;
     $this->filename = "/" . CPHPCache::GetPath($uniq_str);
     $this->TTL = $TTL;
     $this->uniq_str = $uniq_str;
     $this->vars = false;
     if ($TTL <= 0) {
         return false;
     }
     if (is_object($USER) && $USER->CanDoOperation('cache_control')) {
         if (isset($_GET["clear_cache_session"])) {
             if (strtoupper($_GET["clear_cache_session"]) == "Y") {
                 $_SESSION["SESS_CLEAR_CACHE"] = "Y";
             } elseif (strlen($_GET["clear_cache_session"]) > 0) {
                 unset($_SESSION["SESS_CLEAR_CACHE"]);
             }
         }
         if (isset($_GET["clear_cache"]) && strtoupper($_GET["clear_cache"]) == "Y") {
             return false;
         }
     }
     if (isset($_SESSION["SESS_CLEAR_CACHE"]) && $_SESSION["SESS_CLEAR_CACHE"] == "Y") {
         return false;
     }
     $arAllVars = array("CONTENT" => "", "VARS" => "");
     if (!$this->_cache->read($arAllVars, $this->basedir, $this->initdir, $this->filename, $this->TTL)) {
         return false;
     }
     $GLOBALS["CACHE_STAT_BYTES"] += $this->_cache->read;
     $this->content = $arAllVars["CONTENT"];
     $this->vars = $arAllVars["VARS"];
     return true;
 }