1

I calculate a value which is based on configuration settings and on the tax rates.

How can I cache it in a way that it automatically gets cleared when one of those infos changes?

Is this all about using the right tags and then it will get cleared automatically?

2 Answers 2

2

For the configuration part you can add the config cache tag when saving the item:

Mage::app()->saveCache($data, $id, array(
    Mage_Core_Model_Config::CACHE_TAG
), $lifeTime=false);

which will delete your cached item every time this tag gets cleared.

Unfortunately this does not extend to the tax settings and there is no tax specific tag. Instead you can create your own cache tag (MY_TAX_CACHE_TAG).

Mage::app()->saveCache($data, $id, array(
    Mage_Core_Model_Config::CACHE_TAG,
    'MY_TAX_CACHE_TAG'
), $lifeTime=false);

Additionally you would need to create an observer on tax_settings_change_after.

class Observer
{
    public function taxSettingsChangeAfter($observer)
    {
        Mage::app()->cleanCache('MY_TAX_CACHE_TAG');
    }   
}
1
  • @alex thanks for making the implied more obvious with the edit Commented Feb 7, 2013 at 21:59
0

This is a strategy for saving an item to cache, and expiring it after some time (in this example 10800 seconds, or 3 hours).

$mydataforcache = "My data for cache";
$cache_name = "mycachename";
$CACHEDATA = MAGE::app()->loadCache($cache_name);
if (isset($CACHEDATA) && ($CACHEDATA != "")){
// Use data
echo $CACHEDATA;
} else {
// Save data to cache
Mage::app()->saveCache($mydataforcache, $cache_name, array(), 10800);
}

source: http://www.g31zone.com/?tag=magento-save-data-to-cache

To clear this specific value from cache:

 To remove individual values from the cache, we use:

 $cache->remove("mycachename");

source: http://www.nicksays.co.uk/developers-guide-magento-cache/

See also: http://alanstorm.com/magento_config_a_critique_and_caching

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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