How to get Magento cache refreshed by key?

How to get Magento cache refreshed by key?

July 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get Magento cache refreshed by key?

Refreshing Magento Cache by Key: A Comprehensive Guide

As a senior developer working extensively with Magento, I often encounter situations where cache refresh becomes crucial for optimal performance. One such scenario involves refreshing the COLLECTION_DATA cache when new data is inserted or updated in your custom extension.

In this post, we will explore how to refresh specific cache keys associated with our custom logic. We’ll delve into the code structure you provided and enhance it to meet your requirements.

Understanding Your Existing Code

You’ve shared a snippet that utilizes Magento’s caching mechanism quite effectively:

$this->_usecache = Mage::app()->useCache('collections');
if ($this->_usecache){
    $cache = Mage::app()->getCache();
    $key = "mycategory".$this->_config['rootid'];
    $this->tmpAllItems = $cache->load($key);
} else {
    $this->tmpAllItems = false;
}
...

This code checks if caching is enabled for the collections cache type. If enabled, it loads data from the cache using a specific key (mycategory{$rootid}). If not enabled or no cached data exists, it falls back to retrieving fresh data from the database.

The Challenge: Refreshing the Cache on Demand

Your primary goal is to refresh this cache when Magento’s Mage_Catalog_Model_Category::CACHE_TAG is cleaned. Let’s address this challenge step by step.

Step 1: Identifying the Cache Instance Issue

You mentioned that using $cache = Mage::app()->getCacheInstance(); instead of $cache = Mage::app()->getCache(); resolved the cleaning issue for you. This discrepancy lies in the way Magento handles cache instances and cache pools.

The Mage_Core_Model_Cache_Backend_Pooled class, which is used by default, maintains a pool of cache instances to optimize performance. When you use $cache = Mage::app()->getCache();, you’re working with a pooled instance that might not always reflect the actual cache state.

In contrast, $cache = Mage::app()->getCacheInstance(); retrieves an instance of Mage_Core_Model_Cache_Backend_Pooled directly from the pool. This ensures you work with the same instance throughout your script execution.

Step 2: Refreshing the Cache by Tag

To achieve the desired cache refresh, we need to associate our custom cache key with a specific tag that can be cleaned independently:

$cache->save(serialize($this->tmpAllItems), $key, array('my_cache_tag'));

In this updated code snippet, we introduce a new tag called my_cache_tag when saving the serialized data into the cache. This tag acts as an identifier for our custom cache key.

Step 3: Clearing Cached Data by Tag

To refresh the cache associated with our custom logic, we need to clear all entries linked to the my_cache_tag:

$cache = Mage::app()->getCache();
foreach ($cache->getTags() as $tag) {
    if (strpos($tag, 'my_cache_tag') !== false) {
        $ids = $cache->getIdsMatchingAnyTags(array($tag));
        foreach ($ids as $id) {
            $cache->remove($id);
        }
    }
}

This code iterates through all cache tags and checks for the presence of my_cache_tag. If a match is found, it retrieves all associated IDs using getIdsMatchingAnyTags() and clears them using remove().

Conclusion

Refreshing Magento’s cache by key requires a strategic approach that leverages the built-in caching mechanisms effectively. By associating your custom cache keys with specific tags and utilizing the appropriate cache instances, you can achieve precise control over when and what data is refreshed.

Remember to use this technique judiciously, as excessive or improper cache clearing can degrade performance. Always ensure that your code follows best practices for data retrieval and storage to maintain optimal Magento performance.

For more insights into Magento development best practices, feel free to explore our resources at Laravel Company.

Catalog Cache Database Performance

Our network