Skip to content

Commit

Permalink
add auto-warming for products, stub categories and cms pages
Browse files Browse the repository at this point in the history
  • Loading branch information
aheadley committed Jan 11, 2013
1 parent 4d6adb4 commit bfced15
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
52 changes: 52 additions & 0 deletions app/code/community/Nexcessnet/Turpentine/Helper/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public function addUrlToCrawlerQueue( $url ) {
* @return int
*/
public function addUrlsToCrawlerQueue( array $urls ) {
// TODO: remove this debug message
if( $this->getCrawlerDebugEnabled() ) {
foreach( $urls as $url ) {
Mage::helper( 'turpentine/debug' )->log(
'Adding URL to queue: %s', $url );
}
}
$oldQueue = $this->_readUrlQueue();
$newQueue = array_unique( array_merge( $oldQueue, $urls ) );
$this->_writeUrlQueue( $newQueue );
Expand Down Expand Up @@ -166,6 +173,51 @@ public function getAllUrls() {
return array_unique( $urls );
}

/**
* Add URLs to the queue by product model
*
* @param Mage_Catalog_Model_Product $product
* @return int
*/
public function addProductToCrawlerQueue( $product ) {
$productUrls = array();
$origStore = Mage::app()->getStore();
foreach( Mage::app()->getStores() as $storeId => $store ) {
Mage::app()->setCurrentStore( $store );
$baseUrl = $store->getBaseUrl(
Mage_Core_Model_Store::URL_TYPE_LINK );
$productUrls[] = $product->getProductUrl();
foreach( $product->getCategoryIds() as $catId ) {
$cat = Mage::getModel( 'catalog/category' )->load( $catId );
$productUrls[] = rtrim( $baseUrl, '/' ) . '/' .
ltrim( $product->getUrlModel()
->getUrlPath( $product, $cat ), '/' );
}
}
Mage::app()->setCurrentStore( $origStore );
return $this->addUrlsToCrawlerQueue( $productUrls );
}

/**
* Add URLs to the queue by category model
*
* @param Mage_Catalog_Model_Category $category
* @return int
*/
public function addCategoryToCrawlerQueue( $category ) {
// TODO: implement this
}

/**
* Add URLs to queue by CMS page ID
*
* @param int $cmsPageId
* @return int
*/
public function addCmsPageToCrawlerQueue( $cmsPageId ) {
// TODO: implement this
}

/**
* Get the crawler URL queue from the cache
*
Expand Down
29 changes: 23 additions & 6 deletions app/code/community/Nexcessnet/Turpentine/Model/Observer/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ public function banClientEsiCache( $eventObject ) {
*/
public function banProductPageCache( $eventObject ) {
if( Mage::helper( 'turpentine/varnish' )->getVarnishEnabled() ) {
$productUrl = $eventObject->getProduct()->getUrlKey();
$result = $this->_getVarnishAdmin()->flushUrl( $productUrl );
$product = $eventObject->getProduct();
$result = $this->_getVarnishAdmin()->flushUrl( $product->getUrlKey() );
Mage::dispatchEvent( 'turpentine_ban_product_cache', $result );
$this->_checkResult( $result );
$cronHelper = Mage::helper( 'turpentine/cron' );
if( $this->_checkResult( $result ) &&
$cronHelper->getCrawlerEnabled() ) {
$cronHelper->addProductToCrawlerQueue( $product );
}
}
}

Expand Down Expand Up @@ -116,6 +120,7 @@ public function banProductPageCacheCheckStock( $eventObject ) {
$parentProduct = Mage::getModel( 'catalog/product' )
->load( $parentId );
$urlPatterns[] = $parentProduct->getUrlKey();
// TODO: need to queue the parentProducts too
}
$product = Mage::getModel( 'catalog/product' )
->load( $item->getProductId() );
Expand All @@ -124,7 +129,11 @@ public function banProductPageCacheCheckStock( $eventObject ) {
$result = $this->_getVarnishAdmin()->flushUrl( $pattern );
Mage::dispatchEvent( 'turpentine_ban_product_cache_check_stock',
$result );
$this->_checkResult( $result );
$cronHelper = Mage::helper( 'turpentine/cron' );
if( $this->_checkResult( $result ) &&
$cronHelper->getCrawlerEnabled() ) {
$cronHelper->addProductToCrawlerQueue( $product );
}
}
}
}
Expand All @@ -143,7 +152,11 @@ public function banCategoryCache( $eventObject ) {
$category = $eventObject->getCategory();
$result = $this->_getVarnishAdmin()->flushUrl( $category->getUrlKey() );
Mage::dispatchEvent( 'turpentine_ban_category_cache', $result );
$this->_checkResult( $result );
$cronHelper = Mage::helper( 'turpentine/cron' );
if( $this->_checkResult( $result ) &&
$cronHelper->getCrawlerEnabled() ) {
$cronHelper->addCategoryToCrawlerQueue( $category );
}
}
}

Expand Down Expand Up @@ -198,7 +211,11 @@ public function banCmsPageCache( $eventObject ) {
$pageId = $eventObject->getDataObject()->getIdentifier();
$result = $this->_getVarnishAdmin()->flushUrl( $pageId . '\.html$' );
Mage::dispatchEvent( 'turpentine_ban_cms_page_cache', $result );
$this->_checkResult( $result );
$cronHelper = Mage::helper( 'turpentine/cron' );
if( $this->_checkResult( $result ) &&
$cronHelper->getCrawlerEnabled() ) {
$cronHelper->addCmsPageToCrawlerQueue( $pageId );
}
}
}

Expand Down

0 comments on commit bfced15

Please sign in to comment.