1

I want to flush the cache by code and I tried these in my test folder:

<?php
include '/var/www/myfolder/mage_classes.php';
Mage::app()->getCacheInstance()->flush();

or

Mage::app()->getCache()->clean();

none of them cleared the cache I also used

try {
    $cacheTypes = Mage::app()->useCache();
    foreach ($cacheTypes as $type => $option) {
        Mage::app()->getCacheInstance()->cleanType($type);
    }
} catch (Exception $e) {
    Mage::log($e->getMessage());
}

also in my back end admin all cache options are enabled

and again no result when I run this code no error willl be shown and it seems that it will run but with no effect

  • What is mage_classes.php? You could try including /var/www/myfolder/app/Mage.php? Anything in your error logs? enable error reporting in your script to double check. – Douglas Radburn Jun 26 '14 at 16:18
  • it shouldn't be a problem but let me check because it knows every thing i write there if it wasn't it would show me error for example if I change the flush function to flu it will tell function not exists so it is not a problem – Nickool Jun 26 '14 at 16:20
  • yes I checked as I guessed it wasn't a problem because in mage_classes I had already the mage.php – Nickool Jun 26 '14 at 16:22
  • my errors are enabled – Nickool Jun 26 '14 at 16:23
  • How do you know the cache is not cleared? Maybe the cleanType() method only invalidates the cache? – Fabian Blechschmidt Jun 26 '14 at 22:23
1

You should really use Magerun. It's a very powerful tool that allows you to run a lot of basic Magento commands by command line. Very useful especially when you cannot access your backend.

More information: http://magerun.net/

0

While it is a cron'd job to clear only invalidated caches, it is working code

<?php

/************************
 * Find invalidated cache types and refresh
 *
 * Set Cron Time for refresh in config.xml
 *
 */

class Chief_Rcache_Model_Observer {
    public function refreshCache() {
        try {
            $types = Mage::app()->getCacheInstance()->getInvalidatedTypes();
            foreach($types as $type) {
                Mage::app()->getCacheInstance()->cleanType($type->getId());
            }
            Mage::log('Invalid Cache Types Refreshed');
        } catch (Exception $e) {
            Mage::logException($e);
        }
    }
}

Config:

<?xml version="1.0"?>
<config>
    <modules>
        <Chief_Rcache>
            <version>0.1.0</version>
        </Chief_Rcache>
    </modules>
    <global>
        <models>
            <refresh_cache>
                <class>Chief_Rcache_Model</class>
            </refresh_cache>
        </models>
    </global>
    <crontab>
        <jobs>
            <refresh_cache>
                <!-- Min Hour Day Month DoW -->
                <schedule><cron_expr>5 */3 * * *</cron_expr></schedule>
                <run><model>refresh_cache/observer::refreshCache</model></run>
            </refresh_cache>
        </jobs>
    </crontab>
</config>

Module enable file:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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