1

When I update a product the cache becomes invalid. Is it possible to make Magento auto update the cache when saving a product?

I tried searching and the results I got seemed to either be for pre-1.8 Magento or missed som details on how to implement it:

2

I use Bitnami Magento Stack, for me this solution below was the best I have found.

I've tried to create a Mage_Shell_Class php file, but without success (invalid cache array were always empty when it runs, no matter what, and I really don't imagine why).

I've created a php file 'sample.php':

<?php

require 'app/Mage.php';

$invalid = Mage::app()->getCacheInstance()->getInvalidatedTypes();

foreach($invalid as $i)
{
    Mage::app()->getCacheInstance()->cleanType($i["id"]);
}

I've placed it on magento root folder, and to start it I use a cronjob that runs under root user.

So, to create the cronjob on the root user:

sudo crontab -u root -e

And this was my command line to run it:

* * * * * . /opt/bitnami/scripts/setenv.sh ; /opt/bitnami/php/bin/php /opt/bitnami/apps/magento/htdocs/sample.php >> /var/log/cron/cron.log 2>&1

Some parts on this line are very particular to my problem:

  1. Since it just refreshes the invalidated caches I've decided to run it every minute.
  2. setenv.sh is a script that helps me to set the environment when dealing with this particular bitnami stack.
  3. To get the output of this script I used this last part ">> /var/log/cron/cron.log 2>&1" to output errors to a directory that I've created (/var/log/cron), and has given the correct permissions on it.

Probably you need to change the cron line command, but I think this will help you.

1

You could write an observer on the catalog_product_save_after event. It would look something like below cleaning whichever cache you want to refresh

class [Namespace]_[Module]_Model_Observer
{
    public function clearCache($observer)
    {
        foreach (array('block_html','collections','fpc') as $type) // or any type of cache you want to invalidate
        {
            Mage::app()->getCacheInstance()->cleanType($type);
            Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
        }

        return $this;
    }
}
  • hi sander,can you please describe – Amit Bera Nov 10 '14 at 18:55
0

You cant simply refresh the cache, as there is a whole list of cache entries which needs to get filled.

The easiest way to refresh would be to call all the urls having entries related to this, but this is contra productive, as this would cause a whole lot of system usage.

another way would be to manually create all the entries, but thats hard because of all the different context it gets used like block caches depending on templates, layout.xml, storeIDs and so on. Or manual created caches depending on them. Or Caches like related products, cross selling and so on.

There is simply to much for a manual automatic cache refresh.

If you have to much which needs caching, a better way would be to improve the index, or create a customized Index better fitting you needs

0

You can also use another Method for re-indexing All block after saving product.Crate the Observer of the catalog_product_save_after Follow the below code.

Step 1: Crate the configuration file in app\etc\modules\Keyul_UpdateAllChache.xml.xml and place the below code.

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

Step 2: Create the Module Structure as follow folders.

app\code\local\Keyul\UpdateAllChache\etc

app\code\local\Keyul\UpdateAllChache\Model

Step 3: Create config.xml file in app\code\local\Keyul\UpdateAllChache\etc\config.xml and place below code.

<?xml version="1.0"?>
<config>
    <modules>
        <Keyul_UpdateAllChache>
            <version>0.1.0</version>
        </Keyul_UpdateAllChache>
    </modules>
<global>    
    <models>
        <updateallchache>
            <class>Keyul_UpdateAllChache_Model</class>
            <resourceModel>updateallchache_mysql4</resourceModel>
        </updateallchache>
    </models>
    <events>
        <catalog_product_save_after> <!-- identifier of the event we want to catch -->
            <observers>
                <catalog_product_save_after_handler> <!-- identifier of the event handler -->
                    <type>model</type> <!-- class method call type; valid are model, object and singleton -->
                    <class>updateallchache/observer</class> <!-- observers class alias -->
                    <method>updateAll</method>  <!-- observer's method to be called -->
                    <args></args> <!-- additional arguments passed to observer -->
                </catalog_product_save_after_handler>
            </observers>
        </catalog_product_save_after>
    </events>
</global>
</config> 

Step 4: Create Observer.php file in app\code\local\Keyul\UpdateAllChache\Model\Observer.php & place the below code.

<?php
class Keyul_UpdateAllChache_Model_Observer
{

        public function updateAll(Varien_Event_Observer $observer)
        {
            Mage::app()->cleanCache();
        }

}

Step 5: Run your Home Page and try to save a product.

let me know if you have any Query then.

  • The question was about cache, not index. And reindexing everything after a product has changed is an incredibly bad idea, if you have more than a hand full of products. – Fabian Schmengler Nov 10 '14 at 22:35
  • @fschmengler sorry in hurry I will update my post. – Keyul Shah Nov 11 '14 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.