/** * Минификация яваскриптов * * @return void */ public function jsAction() { $this->_response->setHeader('Content-Type', 'text/javascript', true); if (isset($this->_params['files'])) { $files = explode(',', $this->_params['files']); foreach ($files as $key => $file) { $file = $this->_jsdir . trim($file) . '.js'; if (file_exists($file)) { $files[$key] = $file; } } if (!empty($files)) { $cacheid = 'minify_js_' . md5(implode(',', $files)); $this->_cache->setMasterFiles($files); if ($this->_cache->test($cacheid)) { $this->_response->setBody($this->_cache->load($cacheid)); } else { require_once 'Phorm/Plugin/jsmin.php'; $str = ''; foreach ($files as $file) { $str .= file_get_contents($file) . PHP_EOL; } $js = JSMin::minify($str); $this->_cache->save($js, $cacheid); $this->_response->setBody($js); } } } }
/** Perform a curl request based on url provided * @access public * @param string $method * @param array $params * @return type */ public function get($method, $params) { $url = $this->createUrl($method, $params); if (!$this->_cache->test(md5($url))) { $config = array('adapter' => 'Zend_Http_Client_Adapter_Curl', 'curloptions' => array(CURLOPT_POST => true, CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"], CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_LOW_SPEED_TIME => 1)); $client = new Zend_Http_Client($url, $config); $response = $client->request(); $data = $response->getBody(); $this->_cache->save($data); } else { $data = $this->_cache->load(md5($url)); } return Zend_Json_Decoder::decode($data, Zend_Json::TYPE_OBJECT); }
/** * * @param string $id * @return boolean|array */ public function get($id = '') { if ($id == '') { $id = $this->_id; } if ($id == '') { throw new ZendT_Exception('Favor informar o id do cache!'); } $data = $this->_cache->load($id); if (!$data) { return false; } return unserialize($data); }
/** Get data from the endpoint * @access protected * @return string */ protected function getData() { $key = md5($this->_uri); if (!$this->_cache->test($key)) { $graph = new EasyRdf_Graph(self::URI . $this->_uri . self::SUFFIX); $graph->load(); $data = $graph->resource(self::URI . $this->_uri); $this->_cache->save($data); } else { $data = $this->_cache->load($key); } EasyRdf_Namespace::set('dcterms', 'http://purl.org/dc/terms/'); EasyRdf_Namespace::set('pleiades', 'http://pleiades.stoa.org/places/vocab#'); return $data; }
public function cacheRenderedPage($cache, $options) { // If the options are a Zend_Config object, convert to an array if ($options instanceof Zend_Config) { $options = $options->toArray(); } // If the lifetime has been configured use it else default it. if (isset($options['lifetime'])) { $this->_zendPageCacheDuration = $options['lifetime']; } else { $this->_zendPageCacheDuration = 1800; // half an hour } // If the key has been configured use it else default it. if (isset($options['key'])) { $this->_zendPageCacheKey = $options['key']; } else { $this->_zendPageCacheKey = $_SERVER['REQUEST_URI']; } // Prepend the key to prevent namespace collisions and // remove unsupported chars $this->_zendPageCacheKey = 'ZtalPage' . str_replace('/', '', $this->_zendPageCacheKey); // Store the cache object $this->_zendPageCache = $cache; // Check if the cache item exists and, if it does, fetch it $this->_zendPageCacheContent = $this->_zendPageCache->load($this->_zendPageCacheKey); // return whether a valid cache item could be fetched. return $this->_zendPageCacheContent != false; }
/** * Connects to the API and logs in * * @param string $rest_url * @param string $username * @param string $password * @param string $md5_password * @return boolean */ function connect($rest_url = null, $username = null, $password = null, $md5_password = true) { if ($this->cache && ($this->session = $this->cache->load('suiteSession'))) { $this->logged_in = TRUE; } if (!$this->logged_in) { if (!is_null($rest_url)) { $this->rest_url = $rest_url; } if (!is_null($username)) { $this->username = $username; } if (!is_null($password)) { $this->password = $password; } $this->logged_in = FALSE; if ($this->login($md5_password)) { $this->logged_in = TRUE; if ($this->cache) { $this->cache->save($this->session, 'suiteSession'); } } } return $this->logged_in; }
/** Get the data for a postcode * @access public * @param string $postcode * @return array * @throws Pas_Geo_Exception */ public function getData($postcode) { if ($this->_validator->isValid($postcode)) { $postcode = str_replace(' ', '', $postcode); } else { throw new Pas_Geo_Exception('Invalid postcode sent'); } $key = md5($postcode); if (!$this->_cache->test($key)) { $response = $this->_get($postcode); $this->_cache->save($response); } else { $response = $this->_cache->load($key); } $geo = json_decode($response); return array('lat' => $geo->wgs84_lat, 'lon' => $geo->wgs84_lon); }
/** * Metodo que faz limpeza do Cache (Memcached ou Filesystem) * das duas instancias de cache do MPE: cache_FS e cache_acl * * Como chamar via admim: * * http://[mpe-dominio]/cache/clean-all-caches * * @author esilva * * @param Zend_Cache $cache */ public function cleanAllCachesAction(Zend_Cache $cache = null) { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(); $this->cache = Zend_Registry::get('cache_FS'); $this->cache2 = Zend_Registry::get('cache_acl'); echo "<br>Inserindo conteudo no Cache (Memcached ou Filesystem) <br><br>"; $this->cache->save('conteudo variavel teste eh este!!', 'teste'); $testando = $this->cache->load('teste'); $this->cache2->save('conteudo variavel testeAcl eh este!!', 'testeAcl'); $testandoAcl = $this->cache2->load('testeAcl'); sleep(1); echo ""; var_dump('teste_var (cache): ', $testando); var_dump('teste_var_acl (cache): ', $testandoAcl); echo "<br><BR>LIMPANDO CACHE!!!<br><BR>"; // clean all records $this->cache->clean(Zend_Cache::CLEANING_MODE_ALL); $this->cache2->clean(Zend_Cache::CLEANING_MODE_ALL); // clean only outdated $this->cache->clean(Zend_Cache::CLEANING_MODE_OLD); $this->cache2->clean(Zend_Cache::CLEANING_MODE_OLD); sleep(2); echo "<br> [Verificação da limpeza do cache]<Br><BR>"; $testando = $this->cache->load('teste'); $testando2 = $this->cache->load('teste2'); $testando3 = $this->cache->load('teste3'); $testandoAcl = $this->cache2->load('testeAcl'); //recupera do cache if ($testando == false) { echo "variavel [teste] não mais existe<br>"; echo "variavel [teste2] não mais existe<br>"; echo "variavel [teste3] não mais existe<br>"; echo "variavel [testeAcl] não mais existe<br>"; } else { echo "variavel [teste] existe no cache: "; echo "teste: " . $testando . "<br>"; echo "variavel [teste2] existe no cache: "; echo "teste2: " . $testando2 . "<br>"; echo "variavel [teste3] existe no cache: "; echo "teste3: " . $testando3 . "<br>"; echo "variavel [testeAcl] existe no cache: "; echo "testeAcl: " . $testandoAcl . "<br>"; } }
/** * Load Table Names * * Looks at the DB and produces an array of valid table names. * * @return void */ private function _loadTableNames() { if ($this->_cache && ($tableNames = $this->_cache->load('rsc_simpledb_tablenames'))) { $this->_tableNames = $tableNames; } $this->_tableNames = $this->_db->listTables(); if ($this->_cache) { $this->_saveTableNamesCache(); } }
/** Fetch one result * @access public * @param array $where * @param string $order * @return Object */ public function fetchRow($where = null, $order = null) { $id = md5($where->__toString()); if (!$this->_cache->test($id) || !$this->cache_result) { $result = parent::fetchRow($where, $order); $this->_cache->save($result); return $result; } else { return $this->_cache->load($id); } }
/** Fetch pairs from the model * @access public * @param string $sql * @param array $bind * @return array */ public function fetchPairs($sql, $bind = array()) { $id = md5($sql); if (!$this->_cache->test($id) || !$this->cache_result) { $result = parent::fetchPairs($sql, $bind); $this->_cache->save($result); return $result; } else { return $this->_cache->load($id); } }
/** * magic method for get cache value * * @param string $key the key in the cache container * @param string $prefix the prefix used for grouping * * @return mixed the value in the cache */ public function get($key, $prefix = null) { if (!is_null($prefix)) { $prevPrefix = $this->addPrefix($prefix); } $value = $this->cache->load($key); if (!is_null($prefix)) { // revert to the default prefix $this->setPrefix($prevPrefix); } return $value; }
/** * Get an array of all available queues * * @return array */ public function getQueues() { $queues = $this->_cache->load(self::DEFAULT_QUEUE_KEY); if ($queues === false) { return array(); } $queue_names = array(); foreach ($queues as $queue) { $queue_names[] = $queue['name']; } return $queue_names; }
/** * Tests if the password appears on a (weak) password list. The list should * be a simpe newline separated list of (lowercase) passwords. * * @param string $parameter Filename of the password list, relative to APPLICATION_PATH * @param string $password The password */ protected function inPasswordList($parameter, $password) { if (empty($parameter)) { return; } if ($this->cache) { $passwordList = $this->cache->load('weakpasswordlist'); } if (empty($passwordList)) { $filename = __DIR__ . '/../../../docs/' . ltrim($parameter, '/'); if (!file_exists($filename)) { throw new \Gems_Exception("Unable to load password list '{$filename}'"); } $passwordList = explode("\n", file_get_contents($filename)); if ($this->cache) { $this->cache->save($passwordList, 'weakpasswordlist'); } } if (null === $password) { $this->_addError($this->translate->_('should not appear in the list of common passwords')); } elseif (in_array(strtolower($password), $passwordList)) { $this->_addError($this->translate->_('appears in the list of common passwords')); } }
/** * Test if a cache is available for the given id and (if yes) return it (false else) * * Pass true as second param if you cached data that does not pass the is_string() check * * @param string $id Cache ID * @param boolean $unserialize Automatically unserialize the cached result * @return mixed * @throws coding_exception */ public function load($id, $unserialize = false) { if ($this->cache === false) { return false; } try { $data = $this->cache->load($id); // Unserialize if needed if ($data and $unserialize) { $data = unserialize($data); } return $data; } catch (Zend_Cache_Exception $e) { throw new coding_exception('Zend Cache Error: ' . $e->getMessage()); } }
/** Reverse geocode for woeid and other data * @param float $lat * @param float $lon * @return array|boolean */ public function reverseGeoCode($lat, $lon) { if (!is_null($lat) && !is_null($lon)) { $key = 'geocode' . md5($lat . $lon); if (!($place = $this->_cache->load($key))) { $yql = 'SELECT * FROM geo.placefinder where text="' . $lat . ',' . $lon . '" and gflags="R"'; // $yql = 'SELECT * FROM xml WHERE url="http://where.yahooapis.com/geocode?location=' . $lat . '+' . $lon // . '&gflags=R&appid=' . $this->_appID . '"'; $place = $this->_oauth->execute($yql, $this->_accessToken, $this->_accessSecret, $this->_accessExpiry, $this->_handle); $this->_cache->save($place); } else { $place = $this->_cache->load($key); } if (sizeof($place) > 0) { $place = $this->_parser->parseGeocoded($place); return $place; } else { return false; } } else { return false; } }
/** * Return the Translate class. * * If don't exists, try to create it. * * @param string|Zend_Locale $locale Locale/Language to set. * * @return Phprojekt_Language An instance of Phprojekt_Language. */ public function getTranslate($locale = null) { if (null === $locale) { $locale = Phprojekt_User_User::getSetting("language", $this->_config->language); } if (!($translate = $this->_cache->load('Phprojekt_getTranslate_' . $locale))) { $translate = new Phprojekt_Language($locale); $this->_cache->save($translate, 'Phprojekt_getTranslate_' . $locale, array('Language')); } return $translate; }
/** * Module view helper. * * Returns an OntoWiki module either rendered or from cache. * * Fetches the module from the module registry and renders it into the * window template. If a rendering exists in the local module cache it * is used instead. * * @param string $moduleName * @param array $moduleOptions An associative array or and instance of * Zend_config with module options. * The following keys can be used: * enabled – whether the module is enabled or disabled * title – the module window's title * caching – whether the module should be cached * priority – priority of the module in the module contexts * lower number means higher priority * classes – string of css classes for the module window * id – a css id for the module window * * @return string */ public function module($moduleName, $renderOptions = null, $context = OntoWiki_Module_Registry::DEFAULT_CONTEXT) { $moduleRegistry = OntoWiki_Module_Registry::getInstance(); // allow old-style array config if (is_array($renderOptions)) { $renderOptions = new Zend_Config($renderOptions); } // get default options from the registry $defaultModuleOptions = $moduleRegistry->getModuleConfig($moduleName); if ($defaultModuleOptions == null) { $moduleOptions = $renderOptions; } else { if ($renderOptions != null) { $moduleOptions = $defaultModuleOptions->merge($renderOptions); } else { $moduleOptions = $defaultModuleOptions; } } $cssClasses = isset($moduleOptions->classes) ? $moduleOptions->classes : ''; $cssId = isset($moduleOptions->id) ? $moduleOptions->id : ''; $module = $moduleRegistry->getModule($moduleName, $context); // no module found if (null == $module) { return ''; } $module->setOptions($moduleOptions); if ($module->shouldShow()) { // init module view if (null == $this->_moduleView) { $this->_moduleView = clone $this; } $this->_moduleView->clearVars(); // query module's title $this->_moduleView->title = $module->getTitle(); // does the module have a message // TODO: allow multiple messages if (method_exists($module, 'getMessage')) { if ($message = $module->getMessage()) { $this->_moduleView->messages = array($message); } } // does the module have a menu? if (method_exists($module, 'getMenu')) { $menu = $module->getMenu(); $this->_moduleView->menu = $menu->toArray(false, false); } // does the module have a context menu? if (method_exists($module, 'getContextMenu')) { $contextMenu = $module->getContextMenu(); if ($contextMenu instanceof OntoWiki_Menu) { $contextMenu = $contextMenu->toArray(); } $this->_moduleView->contextmenu = $contextMenu; } // is caching enabled if ($this->_moduleCache && $module->allowCaching()) { // get cache id $cacheId = md5($module->getCacheId() . $cssClasses . $this->_config->languages->locale); // cache hit? if (!($moduleContent = $this->_moduleCache->load($cacheId))) { // render (expensive) contents $pre = microtime(true); $moduleContent = $module->getContents(); $post = (microtime(true) - $pre) * 1000; // $this->_owApp->logger->info("Rendering module '$moduleName': $post ms (cache miss)"); // save to cache $this->_moduleCache->save($moduleContent, $cacheId, array('module', $moduleName), $module->getCacheLivetime()); } } else { // caching disabled $pre = microtime(true); $moduleContent = $module->getContents(); $post = (microtime(true) - $pre) * 1000; // $this->_owApp->logger->info("Rendering module '$moduleName': $post ms (caching disabled)"); } // implement tabs if (is_array($moduleContent)) { // TODO: tabs $navigation = array(); $content = array(); $i = 0; foreach ($moduleContent as $key => $content) { $navigation[$key] = array('active' => $i++ == 0 ? 'active' : '', 'url' => '#' . $key, 'name' => $this->_($key)); } $this->_moduleView->navigation = $navigation; $this->_moduleView->content = $moduleContent; } else { if (is_string($moduleContent)) { $this->_moduleView->content = $moduleContent; } } // set variables $this->_moduleView->cssClasses = $cssClasses; $this->_moduleView->cssId = $cssId; if (isset($moduleOptions->noChrome) && (bool) $moduleOptions->noChrome) { // render without window chrome $moduleWindow = $this->_moduleView->render('partials/module.phtml'); } else { // render with window chrome $moduleWindow = $this->_moduleView->render('partials/window.phtml'); } return $moduleWindow; } }