Пример #1
0
 /**
  * Prepare a cache library for use, either by loading it from file, filling it with
  * pre existing data in array form, or leaving it empty and waiting for new entries.
  *
  * @param string $CacheName The name of cache library.
  * @param array|null $ExistingCacheArray An optional array containing an initial seed cache.
  * @param string $CacheMode An optional mode of the cache. Defaults to flat.
  * @return void
  */
 public static function prepareCache($CacheName, $ExistingCacheArray = null, $CacheMode = 'flat')
 {
     // Onetime initialization of in-memory file cache
     if (!is_array(self::$Caches)) {
         self::$Caches = array();
     }
     if ($CacheName != 'locale') {
         return;
     }
     if (!array_key_exists($CacheName, self::$Caches)) {
         self::$Caches[$CacheName] = array('cache' => array(), 'mode' => $CacheMode);
         $UseCache = Gdn::cache()->type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::cache()->activeEnabled();
         if ($UseCache) {
             $CacheKey = sprintf(Gdn_LibraryMap::CACHE_CACHE_NAME_FORMAT, $CacheName);
             $CacheContents = Gdn::cache()->get($CacheKey);
             $LoadedFromCache = $CacheContents !== Gdn_Cache::CACHEOP_FAILURE;
             if ($LoadedFromCache && is_array($CacheContents)) {
                 self::import($CacheName, $CacheContents);
             }
         } else {
             $OnDiskCacheName = sprintf(self::DISK_CACHE_NAME_FORMAT, strtolower($CacheName));
             self::$Caches[$CacheName]['ondisk'] = $OnDiskCacheName;
             // Loading cache for the first time by name+path only... import data now.
             if (file_exists(PATH_CACHE . DS . $OnDiskCacheName)) {
                 $CacheContents = parse_ini_file(PATH_CACHE . DS . $OnDiskCacheName, true);
                 if ($CacheContents != false && is_array($CacheContents)) {
                     self::import($CacheName, $CacheContents);
                 } else {
                     @unlink(PATH_CACHE . DS . $OnDiskCacheName);
                 }
             }
         }
     }
     // If cache data array is passed in, merge it with our existing cache
     if (is_array($ExistingCacheArray)) {
         self::import($CacheName, $ExistingCacheArray, true);
     }
 }