/** * Singleton method * @static * @param string $type * @return Zend_Cache */ public static function factory($type = 'File', $options = array()) { if (!self::$cache_object) { $front_opt = array('automatic_serialization' => true); if (self::$life_time) { $front_opt['lifeTime'] = self::$life_time; } switch ($type) { case 'Memcached': $mhost = $options['memcache_host']; $mport = $options['memcache_port']; $memcache = new Memcache(); if (!@$memcache->connect($mhost, $mport)) { require_once 'Hush/Cache/Exception.php'; throw new Hush_Cache_Exception('Can not connect to memcache server'); } $back_opt = array('servers' => array('host' => $mhost, 'port' => $mport)); break; default: if (!is_dir($options['cache_dir'])) { if (realpath(__DAT_DIR)) { mkdir(realpath(__DAT_DIR) . '/cache', 0600); $options['cache_dir'] = realpath(__DAT_DIR . '/cache'); } else { require_once 'Hush/Cache/Exception.php'; throw new Hush_Cache_Exception('Can not found cache_dir file directory'); } } $back_opt = array('cache_dir' => $options['cache_dir']); break; } self::$cache_object = Zend_Cache::factory('Core', $type, $front_opt, $back_opt); } return self::$cache_object; }
/** * Factory method for cache building * * @param string $type Cache type supported 'File', 'Memcached' now * @return Hush_Cache */ public static function factory($type = 'File', $life_time = 0) { if ($life_time) { Hush_Cache::setLifeTime($life_time); } switch ($type) { case 'Memcached': return Hush_Cache::factory('Memcached', array('memcache_host' => Hush_Cache_Memcache::DEFAULT_HOST, 'memcache_port' => Hush_Cache_Memcache::DEFAULT_PORT)); default: return Hush_Cache::factory('File', array('cache_dir' => __FILECACHE_DIR)); } }