0

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?

1

1 Answer 1

0

Inside the YourModule folder, create the following subfolders: etc, Model, and Cache.

In the etc folder, create a file named config.xml and add the following content:

<?xml version="1.0"?>
<config>
    <modules>
        <YourNamespace_YourModule>
            <version>1.0.0</version>
        </YourNamespace_YourModule>
    </modules>
    <global>
        <models>
            <cache>
                <rewrite>
                    <core>YourNamespace_YourModule_Model_Cache_Core</core>
                </rewrite>
            </cache>
        </models>
    </global>
</config>

In the Model folder, create a file named Cache/Core.php and add the following content

<?php

class YourNamespace_YourModule_Model_Cache_Core extends Mage_Core_Model_Cache
{
    public function clean($tags = array(), $mode = Zend_Cache::CLEANING_MODE_ALL, $specificTags = array())
    {
        // Implement your custom cache cleaning logic here
        
        // Call parent clean method to perform the default cache cleaning
        return parent::clean($tags, $mode, $specificTags);
    }
}

Activate the module: In your Magento installation root, open the app/etc/modules folder. Create a new XML file named YourNamespace_YourModule.xml and add the following content:

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

flush cache. Now, whenever the cache is cleared in Magento, your custom logic defined in the clean method of the YourNamespace_YourModule_Model_Cache_Core class will be executed along with the default cache cleaning process.

1
  • Don't seem to work, have you tested it? Maybe another module is also rewriting it, so in my case it doesn't work, need to check. Also, it shouldn't be "<core>...<cache>...</cache></core>" instead of "<cache>...<core>...</core></cache>" in the config.xml? Commented Jul 5, 2023 at 18:03

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.