Skip to content

Overide cache clean method in custom cache – Magento 1.9

This is more a “Is it possible to do it” instead of “How to do it” type of question.

I have a custom module where I use the Magento 1.9 cache system to store the path of certain files. For example:

Mage::app()->getCache()->save("path/to/file.txt", "MY_CACHE_ID")

So, in my cache, I have something like:

array("MY_CACHE_ID" => "path/to/file.txt");

When I clear it using Mage::app()->getCache()->remove("MY_CACHE_ID") or refresh/clean all cache through admin panel, my cache is correctly deleted BUT my file.txt don’t (as expected).

I want to know if it’s possible to overide my cache’s method used to clear the data of it to be able to delete (unlink) the file stored in it. Or even: delete all files inside the folder /path/to/ whenever the cache is deleted using one of the above clearing methods.

I’ve across an “override option” to put in my module’s config.xml like so:

<config>
    <global>
         <cache>
            <types>
                <my_cache translate="label description" module="module">
                    <label>My Cache</label>
                    <tags>MY_CACHE_TAG</tags>
                    <backend_model>module/cache</backend_model>
                </my_cache>
            </types>
        </cache>
    </global>
</config>

And, in my module/cache model:

<?php

class My_Module_Model_Cache extends Zend_Cache_Backend
{
    protected function _clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
    {
        // Handle here the file deletion
    }
}

But the _clean method wasn’t called.

I’ve also tried to find an event dispatched on cache’s clearing, but didn’t find any.
So, it’s possible to do it?