_expireTimeout = $GLOBALS['sugar_config']['cache_expire_timeout']; if ( isset($GLOBALS['sugar_config']['unique_key']) ) $this->_keyPrefix = $GLOBALS['sugar_config']['unique_key']; } /** * Destructor */ public function __destruct() { } /** * PHP's magic __get() method, used here for getting the current value from the cache. * * @param string $key * @return mixed */ public function __get( $key ) { if ( SugarCache::$isCacheReset ) return; $this->_cacheRequests++; if ( !$this->useLocalStore || !isset($this->_localStore[$key]) ) { $this->_localStore[$key] = $this->_getExternal($this->_keyPrefix.$key); if ( isset($this->_localStore[$key]) ) { $this->_cacheExternalHits++; } else { $this->_cacheMisses++; } } elseif ( isset($this->_localStore[$key]) ) { $this->_cacheLocalHits++; } if ( isset($this->_localStore[$key]) ) { return $this->_localStore[$key]; } return null; } /** * PHP's magic __set() method, used here for setting a value for a key in the cache. * * @param string $key * @return mixed */ public function __set( $key, $value ) { if ( is_null($value) ) { $value = SugarCache::EXTERNAL_CACHE_NULL_VALUE; } if ( $this->useLocalStore ) { $this->_localStore[$key] = $value; } $this->_setExternal($this->_keyPrefix.$key,$value); } /** * PHP's magic __isset() method, used here for checking for a key in the cache. * * @param string $key * @return mixed */ public function __isset( $key ) { return !is_null($this->__get($key)); } /** * PHP's magic __unset() method, used here for clearing a key in the cache. * * @param string $key * @return mixed */ public function __unset( $key ) { unset($this->_localStore[$key]); $this->_clearExternal($this->_keyPrefix.$key); } /** * Reset the cache for this request */ public function reset() { $this->_localStore = array(); SugarCache::$isCacheReset = true; } /** * Reset the cache fully */ public function resetFull() { $this->reset(); $this->_resetExternal(); } /** * Flush the contents of the cache */ public function flush() { $this->_localStore = array(); $this->_resetExternal(); } /** * Returns the number of cache hits made * * @return array assocative array with each key have the value */ public function getCacheStats() { return array( 'requests' => $this->_cacheRequests, 'externalHits' => $this->_cacheExternalHits, 'localHits' => $this->_cacheLocalHits, 'misses' => $this->_cacheMisses, ); } /** * Returns what backend is used for caching, uses normalized class name for lookup * * @return string */ public function __toString() { return strtolower(str_replace('SugarCache','',get_class($this))); } /** * Hook for the child implementations of the individual backends to provide thier own logic for * setting a value from cache * * @param string $key * @param mixed $value */ abstract protected function _setExternal( $key, $value ); /** * Hook for the child implementations of the individual backends to provide thier own logic for * getting a value from cache * * @param string $key * @return mixed $value, returns null if the key is not in the cache */ abstract protected function _getExternal( $key ); /** * Hook for the child implementations of the individual backends to provide thier own logic for * clearing a value out of thier cache * * @param string $key */ abstract protected function _clearExternal( $key ); /** * Hook for the child implementations of the individual backends to provide thier own logic for * clearing thier cache out fully */ abstract protected function _resetExternal(); /** * Hook for testing if the backend should be used or not. Typically we'll extend this for backend specific * checks as well. * * @return boolean true if we can use the backend, false if not */ public function useBackend() { if ( !empty($GLOBALS['sugar_config']['external_cache_disabled']) && $GLOBALS['sugar_config']['external_cache_disabled'] == true ) { return false; } if (defined('SUGARCRM_IS_INSTALLING')) { return false; } if ( isset($GLOBALS['sugar_config']['external_cache_force_backend']) && ( $GLOBALS['sugar_config']['external_cache_force_backend'] != (string) $this ) ) { return false; } return true; } /** * Returns the priority level for this backend * * @see self::$_priority * * @return int */ public function getPriority() { return $this->_priority; } }