سؤال

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?

هل كانت مفيدة؟

المحلول

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');
    }   
}

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top