is it possible to clear things like the products, category, homepage cache individually? If so, how?
2 Answers
EDIT: I should note that this isn't possible via Magento admin interface. You'd have to build/find an extension to add this functionality, else write a script with what I describe below.
-
Cleaning Cache Tags
Magento wraps all communication with the cache backend with Mage_Core_Model_Cache -> Mage::getSingleton('core/cache')
.
If you look at this class you can talk to the backend to remove cached entries using Mage_Core_Model_Cache::clean(array $tags)
. These cache tags should be given for your major Magento blocks. For example, look at Mage_Catalog_Model_Product
:
class Mage_Catalog_Model_Product extends Mage_Catalog_Model_Abstract
{
/**
* Entity code.
* Can be used as part of method name for entity processing
*/
const ENTITY = 'catalog_product';
const CACHE_TAG = 'catalog_product';
protected $_cacheTag = 'catalog_product';
protected $_eventPrefix = 'catalog_product';
protected $_eventObject = 'product';
protected $_canAffectOptions = false;
...
The const CACHE_TAG
is what you'll be looking for. You can clear all product block cache using this tag, like so:
Mage::getSingleton('core/cache')->clean(array('catalog_product'));
As for categories, I don't know of any cache tags implemented for their view classes As Si Griffiths noted, you can use the catalog_category
tags.
As for the homepage, which is a CMS page, the cache tag is actually derived from the CMS page model, Mage_Cms_Model_Page::CACHE_TAG
. The block abstract (Mage_Core_Block_Abstract
) has a method that lets you port over cache tags from a model (see Mage_Core_Block_Abstract::addModelTags
). This pattern is also used for categories and products.
Working with FPC
As mentioned by both Si and myself, FPC adds a layer of complexity to dealing with cache that can't be resolved just by cleaning cache tags. FPC is a separate layer of caching that is stored separately.
To flush ALL FPC, you can use the following:
Enterprise_PageCache_Model_Cache::getCacheInstance()->flush()
Combining that with the above tag cleaning method, you should be able to programmatically get product/category pages to reflect changes. But this FPC command will clear FPC abroad. This is where things like "hole-punching" come into play, but I would say it's beyond the scope of this question and better left to others who have already explained it well:
http://www.kingletas.com/2012/09/how-does-magento-full-page-cache-works.html https://stackoverflow.com/a/9125519/1442685
-
is it possible to find my own specific homepage cache tag from, for example, printing it on the homepage somewhere in our dev enviro? Or if we have a custom homepage that isn't controlled with a cms? Jun 9, 2015 at 17:49
-
1If it's not generated as a CMS page then you are likely using a custom block and template that you want to clear. If your block class doesn't explicitly add cache data (see this article) then it shouldn't be getting cached. If you're using full-page caching, however, that's another story, and you'll have to leverage some kind of hole-punching technique in addition to the above to get the job done. Jun 9, 2015 at 18:01
-
2If you want to see if your block is using cache tags, just call
var_dump($this->getCacheTags())
within your template. This method comes fromMage_Core_Block_Abstract
and should be available to you wherever you are. Jun 9, 2015 at 18:03 -
does this Mage::getSingleton('core/cache')->clean(array('catalog_product')); call the redis clearing function? Jun 11, 2015 at 14:55
-
1@easymoden00b I've updated my answer with the most info I can provide, since the comment thread is getting too long. Jun 18, 2015 at 14:33
Programatically as mentioned above by Rick cache entires can be removed by tag. Most are constructed by tag+id with a slight exception being CMS pages.
So for example:
Products: 'catalog_product' would remove all or 'catalog_product_someidnumeric' would remove that particular id. Categories: 'catalog_category' CMS pages: 'cms_page'
Now that relates to the block cache. FPC in enterprise by default is an all or nothing approach from what I remember. You can clean the lot or nothing. Now extensions for redis or varnish make targeted FPC cleans better but by default you can only clean the lot.
A little tip is the cache tag sometimes matches the class applied to the body tag :)
-
If I were able to create more checkboxes for things like the categories or products or this homepage cache would I be able to clear these individually? Jun 11, 2015 at 14:51
-
-
You can clear FPC this way too. Let's say my homepage is CMS page id is 2. I can run the following command to flush it Enterprise_PageCache_Model_Cache::getCacheInstance()->clean('CMS_PAGE_2'); Jan 18, 2017 at 15:55