Пример #1
0
 /**
  * Discover and return available cache adapter of false if nothing is available
  *
  * @return SugarCache_Abstract|false
  */
 function discover()
 {
     // If the cache is manually disabled, turn it off.
     if (!empty($GLOBALS['sugar_config']['external_cache_disabled']) && true == $GLOBALS['sugar_config']['external_cache_disabled']) {
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log("SugarCache::discover() -- caching explicitly disabled", 'fail');
         }
         $GLOBALS['external_cache_enabled'] = false;
         return SugarCache::factory('Base');
     }
     // check for Zend caching
     if (function_exists("output_cache_get") && empty($GLOBALS['sugar_config']['external_cache_disabled_zend'])) {
         $GLOBALS['external_cache_enabled'] = true;
         $GLOBALS['external_cache_type'] = "zend";
         $cache = SugarCache::factory('Zend');
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log('Found Zend - attempting to use', 'pass');
         }
     } elseif (extension_loaded('memcache') && empty($GLOBALS['sugar_config']['external_cache_disabled_memcache'])) {
         $GLOBALS['external_cache_enabled'] = true;
         $GLOBALS['external_cache_type'] = 'memcache';
         $cache = SugarCache::factory('Memcache');
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log('Found memcache - attempting to use', 'pass');
         }
     } elseif (function_exists("apc_store") && empty($GLOBALS['sugar_config']['external_cache_disabled_apc'])) {
         $GLOBALS['external_cache_enabled'] = true;
         $GLOBALS['external_cache_type'] = "apc";
         $cache = SugarCache::factory('APC');
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log('Found APC - attempting to use', 'pass');
         }
     } elseif (function_exists("zget") && empty($GLOBALS['sugar_config']['external_cache_disabled_smash'])) {
         $GLOBALS['external_cache_enabled'] = true;
         $GLOBALS['external_cache_type'] = "smash";
         $cache = SugarCache::factory('sMash');
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log('Found sMash - attempting to use', 'pass');
         }
     } else {
         // no cache available....return
         $GLOBALS['external_cache_enabled'] = true;
         $GLOBALS['external_cache_type'] = 'base-in-memory';
         $cache = SugarCache::factory('Base');
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log('Found no caching solution - using base');
         }
     }
     // Check the cache.
     if (!$cache->initialized) {
         // Validation failed.  Turn off the external cache and return SugarCache_Base
         $GLOBALS['external_cache_enabled'] = false;
         if (EXTERNAL_CACHE_DEBUG) {
             SugarCache::log("external cache validation check failed...tried cache {$GLOBALS['external_cache_type']}", 'fail');
             SugarCache::log('returning Base');
         }
         return SugarCache::factory('Base');
     }
     // If the cache is being reset, turn it off for this round trip
     $value = '';
     if (isset($GLOBALS['sugar_config']) && isset($GLOBALS['sugar_config']['unique_key'])) {
         $value = $cache->get($GLOBALS['sugar_config']['unique_key'] . 'EXTERNAL_CACHE_RESET');
     }
     if (!empty($value)) {
         // We are in a cache reset, do not use the cache.
         $GLOBALS['external_cache_enabled'] = false;
     } else {
         // Add one to the external cache hits.  This will keep the end user statistics simple.
         // All real checks suceeding will result in 100%.  Otherwise people will be looking for
         // the one check that did not pass.
         $GLOBALS['external_cache_request_external_hits']++;
     }
     return $cache;
 }