Gdn_Cache::activeStore PHP Method

activeStore() public static method

For FileCache, the folder. For Memcache, the server(s).
public static activeStore ( type $ForceMethod = null ) : mixed
$ForceMethod type
return mixed Active Store Location
    public static function activeStore($ForceMethod = null)
    {
        // Get the active cache name
        $ActiveCache = self::activeCache();
        if (!is_null($ForceMethod)) {
            $ActiveCache = $ForceMethod;
        }
        $ActiveCache = ucfirst($ActiveCache);
        // Overrides
        if (defined('CACHE_STORE_OVERRIDE') && defined('CACHE_METHOD_OVERRIDE') && CACHE_METHOD_OVERRIDE == $ActiveCache) {
            return unserialize(CACHE_STORE_OVERRIDE);
        }
        // Use APC cache?
        $apc = false;
        if (c('Garden.Apc', false) && c('Garden.Cache.ApcPrecache', false) && function_exists('apc_fetch')) {
            $apc = true;
        }
        $LocalStore = null;
        $ActiveStore = null;
        $ActiveStoreKey = "Cache.{$ActiveCache}.Store";
        // Check memory
        if (is_null($LocalStore)) {
            if (array_key_exists($ActiveCache, Gdn_Cache::$stores)) {
                $LocalStore = Gdn_Cache::$stores[$ActiveCache];
            }
        }
        // Check APC cache
        if (is_null($LocalStore) && $apc) {
            $LocalStore = apc_fetch($ActiveStoreKey);
            if ($LocalStore) {
                Gdn_Cache::$stores[$ActiveCache] = $LocalStore;
            }
        }
        if (is_array($LocalStore)) {
            // Convert to ActiveStore format (with 'Active' key)
            $Save = false;
            $ActiveStore = array();
            foreach ($LocalStore as $StoreServerName => &$StoreServer) {
                $IsDelayed =& $StoreServer['Delay'];
                $IsActive =& $StoreServer['Active'];
                if (is_numeric($IsDelayed)) {
                    if ($IsDelayed < time()) {
                        $IsActive = true;
                        $IsDelayed = false;
                        $StoreServer['Fails'] = 0;
                        $Save = true;
                    } else {
                        if ($IsActive) {
                            $IsActive = false;
                            $Save = true;
                        }
                    }
                }
                // Add active servers to ActiveStore array
                if ($IsActive) {
                    $ActiveStore[] = $StoreServer['Server'];
                }
            }
        }
        // No local copy, get from config
        if (is_null($ActiveStore)) {
            $ActiveStore = c($ActiveStoreKey, false);
            // Convert to LocalStore format
            $LocalStore = array();
            $ActiveStore = (array) $ActiveStore;
            foreach ($ActiveStore as $StoreServer) {
                $StoreServerName = md5($StoreServer);
                $LocalStore[$StoreServerName] = array('Server' => $StoreServer, 'Active' => true, 'Delay' => false, 'Fails' => 0);
            }
            $Save = true;
        }
        if ($Save) {
            // Save to memory
            Gdn_Cache::$stores[$ActiveCache] = $LocalStore;
            // Save back to APC for later
            if ($apc) {
                apc_store($ActiveStoreKey, $LocalStore, Gdn_Cache::APC_CACHE_DURATION);
            }
        }
        return $ActiveStore;
    }

Usage Example

コード例 #1
0
 /**
  * Reads in known/config servers and adds them to the instance.
  *
  * This method is called when the cache object is invoked by the framework
  * automatically, and needs to configure itself from the values in the global
  * config file.
  */
 public function autorun()
 {
     $Servers = Gdn_Cache::activeStore('memcached');
     if (!is_array($Servers)) {
         $Servers = explode(',', $Servers);
     }
     // No servers, cache temporarily offline
     if (!sizeof($Servers)) {
         SaveToConfig('Cache.Enabled', false, false);
         return false;
     }
     // Persistent, and already have servers. Short circuit adding.
     if ($this->Config(Gdn_Cache::CONTAINER_PERSISTENT) && count($this->servers())) {
         return true;
     }
     $Keys = array(Gdn_Cache::CONTAINER_LOCATION, Gdn_Cache::CONTAINER_PERSISTENT, Gdn_Cache::CONTAINER_WEIGHT, Gdn_Cache::CONTAINER_TIMEOUT, Gdn_Cache::CONTAINER_ONLINE, Gdn_Cache::CONTAINER_CALLBACK);
     foreach ($Servers as $CacheServer) {
         $CacheServer = explode(' ', $CacheServer);
         $CacheServer = array_pad($CacheServer, count($Keys), null);
         $CacheServer = array_combine($Keys, $CacheServer);
         foreach ($Keys as $KeyName) {
             $Value = val($KeyName, $CacheServer, null);
             if (is_null($Value)) {
                 unset($CacheServer[$KeyName]);
             }
         }
         $this->addContainer($CacheServer);
     }
 }
All Usage Examples Of Gdn_Cache::activeStore