Mage::helper('varnishcache')->__('HTML'), self::CONTENT_TYPE_CSS => Mage::helper('varnishcache')->__('CSS'), self::CONTENT_TYPE_JS => Mage::helper('varnishcache')->__('JavaScript'), self::CONTENT_TYPE_IMAGE => Mage::helper('varnishcache')->__('Images') ); return $contentTypes; } /** * Clean Varnish cache * * @param string domain names for cleaning * @param string RegEx pattern for url matching * @param string content type to clean * @return void */ public function clean($domains, $urlRegEx = '.*', $contentType = '.*') { try { $headers = array( self::VARNISH_HEADER_HOST => '^('.str_replace('.', '.', $domains).')$', self::VARNISH_HEADER_REGEX => (empty($urlRegEx) ? '.*' : $urlRegEx), self::VARNISH_HEADER_CONTENT_TYPE => (empty($contentType) ? '.*' : $contentType) ); $this->_purgeCacheServers($headers); Mage::helper('varnishcache')->debug('Purged Varnish items with parameters '.var_export($headers, true)); } catch (Exception $e) { Mage::helper('varnishcache')->debug('Error during purging: '.$e->getMessage()); return false; } return true; } /** * Process all servers */ protected function _purgeCacheServers(Array $headers) { $servers = $this->_getVarnishServers(); if (empty($servers)) { return; } // process all servers foreach ($servers as $server) { // compile url string with scheme, domain/server and port $uri = 'http://'.$server; if ($port = trim(Mage::getStoreConfig(self::XML_PATH_VARNISH_PORT))) { $uri .= ':'.$port; } $uri .= '/'; try { // create HTTP client $client = new Zend_Http_Client(); $client->setUri($uri) ->setHeaders($headers) ->setConfig(array('timeout'=>15)); // send PURGE request $response = $client->request('PURGE'); // check response if ($response->getStatus() != '200') { throw new Exception('Return status '.$response->getStatus()); } } catch (Exception $e) { Mage::helper('varnishcache')->debug('Purging on server '.$server.' failed ('.$e->getMessage().').'); } } } /** * Get Varnish servers for purge * * @return string */ protected function _getVarnishServers() { return explode(';', Mage::getStoreConfig(self::XML_PATH_VARNISH_SERVERS)); } }