Skip to content

Commit

Permalink
Adds varnish as cache type; can enable/disable from system/cache mana…
Browse files Browse the repository at this point in the history
…gement, refresh varnish cache
  • Loading branch information
madalinoprea committed May 10, 2011
1 parent 36f44de commit 13c53ae
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 55 deletions.
21 changes: 12 additions & 9 deletions Magneto_Varnish.xml
@@ -1,9 +1,12 @@
<?xml version="1.0"?>
<config>
<modules>
<Magneto_Varnish>
<active>true</active>
<codePool>community</codePool>
</Magneto_Varnish>
</modules>
</config>
<?xml version="1.0"?>
<config>
<modules>
<Magneto_Varnish>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Adminhtml />
</depends>
</Magneto_Varnish>
</modules>
</config>
10 changes: 9 additions & 1 deletion code/Varnish/Helper/Cacheable.php
Expand Up @@ -3,6 +3,11 @@
class Magneto_Varnish_Helper_Cacheable extends Mage_Core_Helper_Abstract
{

/**
* Retrieves current cookie.
*
* @return Mage_Core_Model_Cookie
*/
public function getCookie()
{
return Mage::app()->getCookie();
Expand All @@ -24,7 +29,10 @@ public function turnOffVarnishCache()

public function turnOnVarnishCache()
{
$this->getCookie()->delete('nocache');
if ($this->getCookie()->get('nocache'))
{
$this->getCookie()->delete('nocache');
}
}

public function quoteHasItems()
Expand Down
40 changes: 15 additions & 25 deletions code/Varnish/Helper/Data.php
Expand Up @@ -3,11 +3,15 @@
class Magneto_Varnish_Helper_Data extends Mage_Core_Helper_Abstract
{

// FIXME: Make use of this option
//public function isModuleEnabled(){
// return Mage::getStoreConfig('varnish/options/enable_purges');
//}
public function useVarnishCache(){
Mage::app()->useCache('varnish');
}

/**
* Return varnish servers from configuration
*
* @return array
*/
public function getVarnishServers()
{
$serverConfig = Mage::getStoreConfig('varnish/options/servers');
Expand All @@ -24,24 +28,11 @@ public function purgeAll()
return $this->purge(array('/.*'));
}

public function purgeUrl($url, $varnishServer, $varnishPort){
$fp = fsockopen($varnishServer, $varnishPort, $errno, $errstr, 2);
if(!$fp){
Mage::log("{$errstr} {$errno}");
} else {
$out = "PURGE " . $url . " . HTTP/1.0\r\n";
$out .= "Host: magento-community.local\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
while(!feof($fp)){
Mage::log( fgets($fp, 128));
}
fclose($fp);
Mage::getBaseHost();
}
}

/**
* Purge an array of urls on all varnish servers.
*
* @param array $urls
*/
public function purge(array $urls)
{
$varnishServers = $this->getVarnishServers();
Expand All @@ -53,9 +44,6 @@ public function purge(array $urls)

foreach ((array)$varnishServers as $varnishServer) {
foreach ($urls as $url) {
//$this->purgeUrl($url, $varnishServer);
//continue; // debuggign stuff

$varnishUrl = "http://" . $varnishServer . $url;

$ch = curl_init();
Expand All @@ -73,6 +61,8 @@ public function purge(array $urls)
do {
$n = curl_multi_exec($mh, $active);
} while ($active);

// FIXME: Add error handling

// Clean up
foreach ($curlHandlers as $ch) {
Expand Down
33 changes: 22 additions & 11 deletions code/Varnish/Model/Observer.php
Expand Up @@ -3,6 +3,9 @@
class Magneto_Varnish_Model_Observer {

/**
* This method is called when http_response_send_before event is triggered to identify
* if current page can be cached and set correct cookies for varnish.
*
* @param $observer Varien_Event_Observer
*/
public function varnish(Varien_Event_Observer $observer)
Expand All @@ -11,6 +14,12 @@ public function varnish(Varien_Event_Observer $observer)
$response = $observer->getResponse();
$helper = Mage::helper('varnish/cacheable'); /* @var $helper Magneto_Varnish_Model_Cacheable */

// Cache disabled in Admin / System / Cache Management
if( !Mage::app()->useCache('varnish') ){
$helper->turnOffVarnishCache();
return false;
}

if( $helper->isNoCacheStable() ){
return false;
}
Expand Down Expand Up @@ -43,23 +52,25 @@ public function varnish(Varien_Event_Observer $observer)
*/
public function purgeCache($observer)
{
// If Varnish is not enabled on admin don't do anything
if (!Mage::app()->useCache('varnish')) {
return;
}

$tags = $observer->getTags();
$urls = array();
Mage::log("Tags: " . get_class($tags) . ' = ' . var_export($tags, true));

if($tags == array())
{
// Mage::log("Tags: " . get_class($tags) . ' = ' . var_export($tags, true));

if ($tags == array()) {
$errors = Mage::helper('varnish')->purgeAll();
if (!empty($errors)) {
Mage::getSingleton('adminhtml/session')->addError(
"Varnish Purge failed");
Mage::getSingleton('adminhtml/session')->addError("Varnish Purge failed");
} else {
Mage::getSingleton('adminhtml/session')->addSuccess(
"The Varnish cache storage has been flushed.");
Mage::getSingleton('adminhtml/session')->addSuccess("The Varnish cache storage has been flushed.");
}
return;
}
return;
}

// compute the urls for affected entities
foreach ((array)$tags as $tag) {
//catalog_product_100 or catalog_category_186
Expand Down
23 changes: 23 additions & 0 deletions code/Varnish/controllers/CacheController.php
@@ -0,0 +1,23 @@
<?php
require_once('Mage/Adminhtml/controllers/CacheController.php');

class Magneto_Varnish_CacheController extends Mage_Adminhtml_CacheController {

/**
* Overwrites Mage_Adminhtml_CacheController massRefreshAction
*/
public function massRefreshAction(){
// Handle varnish type
$types = $this->getRequest()->getParam('types');

if (Mage::app()->useCache('varnish') ) {
if( (is_array($types) && in_array('varnish', $types)) || $types="varnish") {
Mage::helper('varnish')->purgeAll();
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("Varnish cache type purged ."));
}
}

// Allow parrent handle core cache types
parent::massRefreshAction();
}
}
31 changes: 22 additions & 9 deletions code/Varnish/etc/config.xml
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Magneto_Varnish>
<version>0.1.0</version>
<version>0.2.0</version>
</Magneto_Varnish>
</modules>
<frontend>
Expand All @@ -28,13 +28,15 @@
<class>Magneto_Varnish_Helper</class>
</varnish>
</helpers>
<!-- index>
<indexer>
<varnish_pagecache>
<model>varnish/indexer_pagecache</model>
</varnish_pagecache>
</indexer>
</index -->
<cache>
<types>
<varnish translate="label,description" module="varnish">
<label>Varnish</label>
<description>Full page cache in Varnish</description>
<tags></tags>
</varnish>
</types>
</cache>
<events>
<http_response_send_before>
<observers>
Expand All @@ -57,6 +59,18 @@
</events>
</global>

<admin>
<routers>
<adminhtml>
<args>
<modules>
<varnish before="Mage_Adminhtml">Magneto_Varnish</varnish>
</modules>
</args>
</adminhtml>
</routers>
</admin>

<default>
<varnish>
<options>
Expand All @@ -67,7 +81,6 @@
</varnish>
</default>


<adminhtml>
<acl>
<resources>
Expand Down

0 comments on commit 13c53ae

Please sign in to comment.