Magento (How to fix): One or more of the Cache Types are invalidated: Blocks HTML output.

Magento (How to fix): One or more of the Cache Types are invalidated: Blocks HTML output.

Some time has passed since I wrote original version of this article, Magento was at 1.5. these days…

Now, almost 2 years later I have decided to revamp the article since lot’s of feedback is gathered…

For that purpose, I have wrote small extension for it that you can find here:
https://github.com/tomasinchoo/Inchoo_InvalidatedBlockCacheFix/tags

I would like to thank all of you, especially people that gave suggestions in order to improve it.

Main difference is that it is done through observer and the code is wrapped up in extension for easier use.

Keep in mind that this extension is created for purposes of blog article at inchoo.net and as such can only be used as proof of concept. If you find it helpful in order to learn Magento, that is great, but be careful if you plan to use it in production environment even though I do 🙂

Some sites can’t afford to clean block cache on every product edit, so please keep that in mind.

Thanks, and looking forward to your feedback!

You made it all the way down here so you must have enjoyed this post! You may also like:

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

Super easy universal Magento cache extension Srdjan Stojiljkovic
Srdjan Stojiljkovic, | 14

Super easy universal Magento cache extension

Magento block caching Ivan Weiler
Ivan Weiler, | 33

Magento block caching

83 comments

  1. Beside I respect this side and I got some valuable information in the past:
    This approach to the problem is very funny, it is like your wife cheat on you and your solution is to give her a shower.

    If you are saving one product and you have to refresh all the cache because of that, this is not a solution because you are wasting 90% of the cache for just 1 product:
    – If the cache is invalidated it means it will not be used till you refresh it.
    – if you refresh it you loose all the previous cache stored (because refresh in magento means delete )

    A serious approach would be:
    – to find out why the whole cache is invalidated when you save a product ? ( this is for sure a core bug )
    – fix this behavior so that only the cache related to the product you saved is invalidated/refreshed.

    This is what I would call a solution, not hide the alarm and reset the whole thing every time.

    1. Thanks for comment. You are right. Yet, this article is like 7 years old. Before Magento version mentioned in article, Magento didn’t invalidate cache at all (at product save) even though it should have. This is not a bug at all. This is merely notification about current state of Magento cache. That being said… At that point we were working on daily deal sites (you probably remember those). and this was requirement (yes, we used only 1 product active at any given time). Article was created just as proof of concept.(as it was mentioned), and hence the notice that some sites can’t afford clearing the cache on every product save. My bad I didn’t state the obvious: “It clears block cache, maybe it is not good idea for everyone”. 🙂 Anyway, there are people using this so I couldn’t just delete it, but I’ll go check it and see if I can make it more clear on github what is it all about…

  2. “I have decided to revamp the article” means you’re going to post a link to the module code? What happened to the original article? I’d like to actually learn about the issue and how you went about deciding how to solve it.

  3. I’m seeing that the cache is refreshed properly, but for some reason the warning is still appearing, even with the content up to date. Is this happening for anyone else or is it just me?

    1. Hi Dave, this is happening for me too on 1.9.0.1. Did you find anything out? This is the behaviour I see:

      When triggered by event controller_action_postdispatch_adminhtml_catalog_product_save in adminhtml scope (i.e. manual product save in admin interface) then the cache is cleaned and the warning disappears.

      When triggered by event catalog_product_save_commit_after in global scope, the cache appears to be get cleaned but the warning persists. I need to use this because our products are updated by API.

      When triggered by a cron job, the cache appears to be cleaned but the warning persists. I tried this as a workaround.

      I’ve dug around the core code a lot and I can’t figure a reason why this behaviour is happening.

  4. Terrific work! That is the type of info that should be shared around the internet.
    Shame on Google for now not positioning this post upper!
    Come on

    over and consult with my website . Thank you

    =)

  5. @Fulvio … Hm… where exactly did you read that it works only for US?
    https://github.com/tomasinchoo/Inchoo_InvalidatedBlockCacheFix/blob/master/README.md

    Maybe you read that one on this (OTHER) extension: https://github.com/tomasinchoo/Inchoo_USDisableRegions

    Well, in that case I’m afraid you should try some other extension like this one: http://www.magentocommerce.com/magento-connect/remove-or-change-displayed-states-and-regions-1964.html

    My version was created as part of the project on which client had no interest in shipping to any other country than USA so that save us some time and some cost to the client 😉

  6. Hi Tomas i tried to install your app but it gave me some problem, you write in the README file:
    ” ….It works only for US….. ”
    why only for US they look better than european or oceanian african etc… ? 😉

  7. I am pretty new with Magento. How do I even “install” this extension?

    Thanks so much in advance!

  8. We are a gaggle of volunteers and starting a new scheme in our community.
    Your site provided us with helpful info to work on.
    You’ve done a formidable job and our whole community might be thankful to you.

  9. Fantastic goods from you, man. I have understand
    your stuff previous to and you’re just extremely excellent. I really like what you have acquired here, certainly like what you’re saying and the way in which you say it.
    You make it enjoyable and you still care for to keep it sensible.
    I can’t wait to read far more from you. This is really a terrific website.

  10. Thank you for sharing superb informations. Your website is very cool.
    I’m impressed by the details that you have on this web site. It reveals how nicely you

    understand this subject. Bookmarked this website page, will come back for extra articles.

    You, my pal, ROCK! I found just the information I already searched all over the place

    and simply could not come across. What an ideal website.

  11. Already created this little extension myself, a few differences, I made a yes/no enable feature for the backend, in yours you have singleton inside the observer where as mine does not.
    Why should this be needed?

    Rob.

  12. @Alex I could have added it, however, it is ALWAYS invalidated, even if you save product with no NEW data, however I don’t see what can it break, so I will add it as soon as possible 🙂 Thanks for feedback!

  13. Why dont you check at least if cache is invalidated before flushing it? Something like
    $invalidateTypes = Mage::app()->getCacheInstance()->getInvalidatedTypes();

    if (!empty($invalidateTypes[‘block_html’])) {…

  14. @Lewis
    Not really. Magento Cron runs on my server but the cache block still gets invalid. (1.7.0.2)
    Fix from travis worked fine. Thanks!

  15. Hi I have this problem whit html block invalidate, but all
    the documentation and bug error I found is about Magento 1.5 my
    solution is 1.7.0.2 and think I’m the only one whit this problem
    whit the 1.7 Any suggestion?

  16. I am using EE 1.12.0.2 and get this message for full page cache also. How can I add this cache to the refresh also?

  17. this solve issue when you save product in admin, what about product update on frontend?
    i mean if you sell product, QTY would decrease (1st change) and if you run out of stock, product will not be salable (2nd change) and might want to hide it from some blocks.
    so rather than hacking core files, better solution would be to have an observer on product change and reset the cache than.
    thoughts?

  18. I got the same error in 1.7.0.2

    before experimenting, i looked at one of Travis’ last few comments.

    Here’s what worked for me:

    Select all > Disable Cache
    Select all > Enable Cache

    selecting that individual item and re-enabling wont help.

  19. I am running Magento 1.7.0.1. I followed @Travis instructions and it works great. Remember to recompile if your using it.

  20. I got the same error message:
    Fatal error: Call to a member function getCollection() on a non-object in …..code/core/Mage/CatalogRule/Model/Observer.php on line 49.

    I disabled my module in System-Configuration-Advanced
    but it did not change anything. Same error on product save

    Also there are no files under var/report…

    Also tried to comment out all lines in my Rule.php – same error

    Any ideas?

  21. Travis, i have two questions! First…. why is this an problem in magento for many years and never solved? and the seccond question why nobody write an patch… so everybody can solved the problem easy! I use version 1.6.2 and many times if people talk about go to directory … i can’t find the dir coz it isn’t there. My problem is when i add a product i can not saved the short en long description! block-html cash fault. I hope you will help me.
    regards Ronald

  22. Cicilia, perhaps post your code. Also, you could try disabling any other 3rd party extensions you have installed.

  23. Thanks for your patience guys – I’ve just been super slammed busy lately. I do have an implementation of the workflow I discussed before. I’m doing a little further testing on it, and then it should be online! Hopefully before the end of the week…

  24. Yes commented out every line in Rule.php and also tried with blank class but still error exists. Is there some tag missing in config.xml for 1.6.2 (that’s my guess only)?

  25. You’ll have to find which line is causing the error. Presumably it’s in Rule.php. Go through and comment out all the lines and re-enable one by one to find the culprit.

  26. Hi, I implemented it on CE 1.6.2 and getting error exactly as told by Rick. Also, Travis, there is no error report generated for that. Any further suggestions?

  27. Hi Rick, I think you need to trace back the error further. Look in /var/report for the file that relates to the error number being shown. Post the contents or relevant part of the error report.

  28. I implemented exactly this (last post of Travis) on 1.6.2 but I get a Fatal error: Call to a member function getCollection() on a non-object in /home/rick/domains/website/public_html/app/code/core/Mage/CatalogRule/Model/Observer.php on line 49. Any ideas?

  29. SOLVED – As a follow up to the above post:

    I disabled all the caches and tested again. I then received the debug message in the log file – meaning the extension was firing properly.

    I then re-enabled the caches, and everything seems to be working properly.

  30. Hi, I am trying this on CE 1.6.0 and can’t seem to get it to fire.

    Step 1) Create /app/etc/modules/Emerge_Cacherefresh.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Emerge_Cacherefresh>
                <active>true</active>
                <codePool>local</codePool>
            </Emerge_Cacherefresh>
        </modules>
    </config>

    Step 2: Create /app/code/local/Emerge/Cacherefresh/etc/config.xml

    <?xml version="1.0"?>
    <config>
    	 <modules>
            <Emerge_Cacherefresh>
                <version>1.0.0</version>
            </Emerge_Cacherefresh>
        </modules>
    	<global>
    		<models>
    			<catalogrule>
    				<rewrite>
    <rule>Emerge_Cacherefresh_Model_Rule</rule>
    				</rewrite>
    			</catalogrule>	
    		</models>
    	</global>
    
    </config>

    Step 3: Create /app/code/local/Emerge/Cacherefresh/Model/Rule.php

    <?php
    class Emerge_Cacherefresh_Model_Rule extends Mage_CatalogRule_Model_Rule
    {
       /**
         * Apply all price rules to product
         *
         * @param int|Mage_Catalog_Model_Product $product
         * @return Mage_CatalogRule_Model_Rule
         */
        public function applyAllRulesToProduct($product)
        {
            $this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
            $this->_invalidateCache();
    
            //Notice this little line
        Mage::app()->getCacheInstance()->cleanType('block_html');
    	Mage::log('Cache block HTML refresh');
    
            $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price');
            if ($indexProcess) {
                $indexProcess->reindexAll();
            }
        }
    }

    Notice that I have included the Mage::log function to show a debug line.

    All permissions on the files are 777.

    The warning message still appears:
    One or more of the Cache Types are invalidated: Blocks HTML output. Click here to go to Cache Management and refresh cache types.

    And, my debug message is not show in /var/log/system.log

    If I use the DebugToolbar on the front-end, I can see that my “Emerge_Cacherefresh” is being loaded.

    I don’t know how else to check whether it’s working. Could anyone please advise?

  31. Can someone implement this change for me for a small fee? I moved Magento to new server and can’t fix this issue? Thanks.

  32. You would need to override the crontab settings (most likely with xml), and then override the error/warning message value set for this cron action in a custom module. This is screaming custom extension! I’ll let you know when I have one.. maybe I can whip something up over the next couple days in my free time.

  33. I agree with you, Mark. Refreshing cache on every product update is not a good idea. Do you have any idea on how to change the runtime of that cron event, and/or to hide that annoying message?

  34. I think rather than refreshing the cache on every product save, the better alternative is to just remove this notification message, even though the block cache is outdated. While it is considered ‘outdated’, I don’t believe that having a slightly outdated block html warrants an intrusive adminhtml notification message. If you were that concerned and had a lot of product saves and still want to reap the benefits of the cache (and not see that notification message), I would suggest to hide that message and then lower the default runtime of that cron event.

  35. Hi,
    I am using Magento ver. 1.6.2.0. having above problem.
    I am new to programming. Could you please tell the exact location of the config.xml file. I searched this file found in many location. Kindly help me to resolved this issue.
    Thanks is advace
    Jagadeesh

  36. I know this fix is to be implemented at the Admin level but if I have Admin and frontend on separate servers, do I need to apply this fix on frontend as well?

  37. Someone on the Magento forums (I don’t remember where) told me that caches are configured for being refreshed every X hours.

  38. I like the idea of Toni as he said it doesn’t overwrite anything.
    But to be honest, if you have several people working in the catalog backend and/or you have a high traffic website, doing a clean of the HTML Block too often is not a good idea. Between display conflict and page loading, you will have some unhappy customers.

  39. @Ruslan I hope you have been able to find the resolution so for, but if not here it is, you would have to declare your models path explicitly in your global node.
    in your case it should be some thing like this

    <models>
      <class>Fix_FixBlockCacheInvalidated_Model</class>
    </models>
  40. BTW, Vinai, you are a sniper 🙂 as your suggestion about issue with reindexAll() is absolutely right.

    We have found it after the February release too and fixed it. The fix was available starting from Magento 1.6 alpha, and will be included in the soon-coming Magento 1.6 stable release.

  41. Maybe you got confused with my reply on Inchoo’s forum, because I omitted the complete config file:

    <config>
    <modules>
    <Fix_FixBlockCacheInvalidated>
    <version>0.1.0</version>
    </Fix_FixBlockCacheInvalidated>
    </modules>
    <global>
    <models>
    <catalogrule>
    <rewrite>
    <rule>Sioseo_FixBlockCacheInvalidated_Model_Rule</rule>
    </rewrite>
    </catalogrule>
    </models>
    </global>
    </config>

    Hope that helps,

    Adrian

  42. I’m New to coding… How do i create a file called Rule.php in my module Model directory?
    Where exactly do i place the

    <global>
    <models>
    <catalogrule>
        <rewrite>
            <rule>Yourpackage_Yourmodule_Model_Rule</rule>
        </rewrite>
    </catalogrule>
    </models>
    </global>

    ,

    any where in my config.xml?

  43. I’m New to coding… How do i create a file called Rule.php in my module Model directory?
    Where exactly do i place the

    Yourpackage_Yourmodule_Model_Rule

    ,

    any where in my config.xml?

  44. Maybe someone can tell me what I’m doing wrong.

    app/etc/modules/Fix_FixBlockCacheInvalidated.xml

    <config>
        <modules>
            <Fix_FixBlockCacheInvalidated>
                <active>true</active>
                <codePool>local</codePool>
            </Fix_FixBlockCacheInvalidated>
        </modules>
    </config>

    app/code/local/Fix/FixBlockCacheInvalidated/etc/config.xml

    <global>
            <models>
                <catalogrule>
                    <rewrite>
                        <rule>Fix_FixBlockCacheInvalidated_Model_Rule</rule>
                    </rewrite>
                </catalogrule>
            </models>
        </global>

    app/code/local/Fix/FixBlockCacheInvalidated/Model/Rule.php

    class Fix_FixBlockCacheInvalidated_Model_Rule extends Mage_CatalogRule_Model_Rule
    {
       /**
         * Apply all price rules to product
         *
         * @param int|Mage_Catalog_Model_Product $product
         * @return Mage_CatalogRule_Model_Rule
         */
        public function applyAllRulesToProduct($product)
        {
            $this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
            $this->_invalidateCache();
    
            //Notice this little line
        Mage::app()->getCacheInstance()->cleanType('block_html');
    
            $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price');
            if ($indexProcess) {
                $indexProcess->reindexAll();
            }
        }
    }

    Thanks!

  45. Well….unless there is something I don’t know, you will have COMPLETELY cleared block html cache when you save product from admin. How much impact will it have on caching utilisation is depending on how often you are adding /editing product.

  46. OK, I’ve fixed the config.xml file:

    <global>
    <models>
    <catalogrule>
    <rewrite>
    <rule>Sioseo_FixBlockCacheInvalidated_Model_Rule</rule>
    </rewrite>
    </catalogrule>
    </models>
    </global>

    Now, the message about the invalidation doesn’t show anymore, and I see my debug message at the log file, so my code is executing.

    How can I make sure it’s working ok? What was the sympthom, apart from the cache warning?

    Cheers,

    Adrian

  47. (sorry for forgetting to htmlencode the xml tags)

    Hi, Tomas.

    I’ve followed your advice and it doesn’t seem to be called when I modify a product’s description, just to fire the cache invalidation.

    Here’s what I did:

    app/etc/modules/Sioseo_FixBlockCacheInvalidated.xml:
    <config>
    <modules>
    <Sioseo_FixBlockCacheInvalidated>
    <active>true</active>
    <codePool>local</codePool>
    </Sioseo_FixBlockCacheInvalidated>
    </modules>
    </config>

    app/code/local/Sioseo/FixBlockCacheInvalidated/etc/config.xml:
    <config>
    <modules>
    <Sioseo_FixBlockCacheInvalidated>
    <version>0.1.0</version>
    </Sioseo_FixBlockCacheInvalidated>
    </modules>
    <catalogrule>
    <rewrite>
    <rule>Sioseo_FixBlockCacheInvalidated_Model_Rule</rule>
    </rewrite>
    </catalogrule>

    </config>

    app/code/local/Sioseo/FixBlockCacheInvalidated/Model/Rule.php:
    class Sioseo_FixBlockCacheInvalidated_Model_Rule extends Mage_CatalogRule_Model_Rule
    {
    public function applyAllRulesToProduct($product)
    {
    $this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
    $this->_invalidateCache();

    //APLATTE 20110527 – Refrescar la caché de bloque HTML
    Mage::app()->getCacheInstance()->cleanType(‘block_html’);
    Mage::log(‘Caché de bloques HTML refrescado desde Sioseo_FixBlockCacheInvalidated_Model_Rule.applyAllRulesToProduct’);

    $indexProcess = Mage::getSingleton(‘index/indexer’)->getProcessByCode(‘catalog_product_price’);
    if ($indexProcess) {
    $indexProcess->reindexAll();
    }
    }
    }

    Cache still gets invalidated, and my log comment doesn’t show on the log.

    Thanks,

    Adrian

  48. Hi, Tomas.

    I’ve followed your advice and it doesn’t seem to be called when I modify a product’s description, just to fire the cache invalidation.

    Here’s what I did:

    app/etc/modules/Sioseo_FixBlockCacheInvalidated.xml:

    true
    local

    app/code/local/Sioseo/FixBlockCacheInvalidated/etc/config.xml:

    0.1.0

    Sioseo_FixBlockCacheInvalidated_Model_Rule

    app/code/local/Sioseo/FixBlockCacheInvalidated/Model/Rule.php:
    class Sioseo_FixBlockCacheInvalidated_Model_Rule extends Mage_CatalogRule_Model_Rule
    {
    /**
    * Apply all price rules to product
    *
    * @param int|Mage_Catalog_Model_Product $product
    * @return Mage_CatalogRule_Model_Rule
    */
    public function applyAllRulesToProduct($product)
    {
    $this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
    $this->_invalidateCache();

    //APLATTE 20110527 – Refrescar la caché de bloque HTML
    Mage::app()->getCacheInstance()->cleanType(‘block_html’);
    Mage::log(‘Caché de bloques HTML refrescado desde Sioseo_FixBlockCacheInvalidated_Model_Rule.applyAllRulesToProduct’);

    $indexProcess = Mage::getSingleton(‘index/indexer’)->getProcessByCode(‘catalog_product_price’);
    if ($indexProcess) {
    $indexProcess->reindexAll();
    }
    }
    }

    Cache still gets invalidated, and my log comment doesn’t show on the log.

    Thanks,

    Adrian

  49. Ah, I see. Hmm, I would believe reindexing a single product should be enough, but probably you are right.

    Also, the method reindexProductIds() is only available on the indexerProcess Resource model, so to correct my previous post, here is how to reindex a single product properly:

    Mage::getSingleton(‘index/indexer’)->processEntityAction($product, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);

  50. @Vinai
    Only thing I did with this rewrite is this this line:
    Mage::app()->getCacheInstance()->cleanType(‘block_html’);
    Anyway… I didn’t took time to check what’s under the hood in $indexProcess object, I just assumed that Magento guys have their reasons for calling reindexAll 🙂

  51. Good find!
    But wouldn’t

    $indexProcess->reindexProductIds($product->getId())

    be enough instead of doing

    $indexProcess->reindexAll();

    ?
    Vinai

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.