/** * setup the cache and add it to zend registry * * @param bool $_enabled disabled cache regardless what's configured in config.inc.php * * @todo use the same config keys as Zend_Cache (backend + frontend) to simplify this */ public static function setupCache($_enabled = true) { if (self::$cacheStatus !== NULL && self::$cacheStatus === $_enabled) { return; } $config = self::getConfig(); if ($config->caching && $config->caching->active) { if (isset($config->caching->shared) && $config->caching->shared === true) { self::set(self::SHAREDCACHE, true); } else { self::set(self::SHAREDCACHE, false); } } // create zend cache if ($_enabled === true && $config->caching && $config->caching->active) { $logging = $config->caching->logging ? $config->caching->logging : false; $logger = self::getLogger(); $frontendOptions = array('lifetime' => $config->caching->lifetime ? $config->caching->lifetime : 7200, 'automatic_serialization' => true, 'caching' => true, 'automatic_cleaning_factor' => 0, 'write_control' => false, 'logging' => $logging, 'logger' => $logger); $backendType = $config->caching->backend ? ucfirst($config->caching->backend) : 'File'; $backendOptions = $config->caching->backendOptions ? $config->caching->backendOptions->toArray() : false; if (!$backendOptions) { switch ($backendType) { case 'File': $backendOptions = array('cache_dir' => $config->caching->path ? $config->caching->path : Tinebase_Core::getTempDir(), 'hashed_directory_level' => $config->caching->dirlevel ? $config->caching->dirlevel : 4, 'logging' => $logging, 'logger' => $logger); break; case 'Memcached': $host = $config->caching->host ? $config->caching->host : (isset($config->caching->memcached->host) ? $config->caching->memcached->host : 'localhost'); $port = $config->caching->port ? $config->caching->port : (isset($config->caching->memcached->port) ? $config->caching->memcached->port : 11211); $backendOptions = array('servers' => array('host' => $host, 'port' => $port, 'persistent' => TRUE)); break; case 'Redis': $host = $config->caching->host ? $config->caching->host : ($config->caching->redis->host ? $config->caching->redis->host : 'localhost'); $port = $config->caching->port ? $config->caching->port : ($config->caching->redis->port ? $config->caching->redis->port : 6379); if ($config->caching && $config->caching->prefix) { $prefix = $config->caching->prefix; } else { if ($config->caching && $config->caching->redis && $config->caching->redis->prefix) { $prefix = $config->caching->redis->prefix; } else { $prefix = $config->database && $config->database->tableprefix ? $config->database->tableprefix : 'tine20'; } } $prefix .= '_CACHE_'; $backendOptions = array('servers' => array('host' => $host, 'port' => $port, 'prefix' => $prefix)); break; default: $backendOptions = array(); break; } } Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . " cache of backend type '{$backendType}' enabled"); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { // logger is an object, that makes ugly traces :) $backendOptionsWithoutLogger = $backendOptions; if (isset($backendOptionsWithoutLogger['logger'])) { unset($backendOptionsWithoutLogger['logger']); } Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " backend options: " . print_r($backendOptionsWithoutLogger, TRUE)); } } else { Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . ' Cache disabled'); $backendType = 'Test'; $frontendOptions = array('caching' => false); $backendOptions = array(); } // getting a Zend_Cache_Core object try { $cache = Zend_Cache::factory('Core', $backendType, $frontendOptions, $backendOptions); } catch (Exception $e) { Tinebase_Exception::log($e); $enabled = false; if ('File' === $backendType && !is_dir($backendOptions['cache_dir'])) { Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . ' Create cache directory and re-try'); if (mkdir($backendOptions['cache_dir'], 0770, true)) { $enabled = $_enabled; } } self::setupCache($enabled); return; } // some important caches Zend_Locale::setCache($cache); Zend_Translate::setCache($cache); Zend_Db_Table_Abstract::setDefaultMetadataCache($cache); self::set(self::CACHE, $cache); self::$cacheStatus = $_enabled; }
/** * get cache from the registry * * @return Zend_Cache_Core the cache */ public static function getCache() { if (!self::get(self::CACHE) instanceof Zend_Cache_Core) { self::$cacheStatus = null; Tinebase_Core::setupCache(); } return self::get(self::CACHE); }