Scheduled Redis cache clean up

Magento, starting from the CE 1.8 and EE 1.13 versions, has a built-in Cm RedisSession extension. This extension helps Magento to use Redis cache for the backend caching and session storage. We can find much information about Redis installation and configuration of the Magento store to make it working with the cache. Often, it is recommended to flush Redis cache daily to avoid huge cache amount and some caching issues that, usually, occur when some part of the dynamic content was changed. Our article describes a simple extension which will be running daily and flushing the whole Redis cache.

First of all, let us make all the required preparation to create an extension – we will call it Atwix_Redisflush. Create a corresponding file in the app/etc/modules. Of course, you can name the extension as you wish, or even include it to some other extension – in that case, just skip the information to this.

<?xml version="1.0"?>
<!--
/**
 * file path
 * magento_root/app/etc/modules/Atwix_Redisflush.xml
 */-->
<config>
    <modules>
        <Atwix_Redisflush>
            <active>true</active>
            <codePool>local</codePool>
        </Atwix_Redisflush>
    </modules>
</config>

Note that we also need a config file for our extension, but only with the model declaration.

<?xml version="1.0"?>
<!--
/**
 * file path 
 * magento_root/app/code/local/Atwix/Redisflush/etc/config.xml
 */
-->
<config>
    <modules>
        <Atwix_Redisflush>
            <version>1.0.0</version>
        </Atwix_Redisflush>
    </modules>
    <global>
        <models>
            <atwix_redisflush>
                <class>Atwix_Redisflush_Model</class>
            </atwix_redisflush>
        </models>
    </global>
</config>

As a result, we have got the extension with the model identifier declaration. Now, let’s add a scheduled cron job, which will call Redis cache flush daily at 2 a.m. by server’s time. Add a “crontab” node after the “global”:

................
    </global>
    <crontab>
        <jobs>
            <atwix_redisflush>
                <schedule>
                    <cron_expr>0 2 * * *</cron_expr>
                </schedule>
                <run>
                    <model>atwix_redisflush/cron::flushRedisCache</model>
                </run>
            </atwix_redisflush>
        </jobs>
    </crontab>
</config>

Use your model identifier in the “model” node. Since, we have not scheduled it yet, let’s create the file itself. It should be called Cron.php (due to our declaration) and has the flushRedisCache method:

<?php

/**
 * Atwix
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.

 * @category    Atwix_Mod
 * @package     Atwix_Redisflush
 * @author      Atwix Core Team
 * @copyright   Copyright (c) 2014 Atwix (http://www.atwix.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * file path
 * magento_root/app/code/local/Atwix/Redisflush/Model/Cron.php
 */

class Atwix_Redisflush_Model_Cron extends Mage_Core_Model_Abstract
{
    public function flushRedisCache()
    {
        try {
            $output = shell_exec('redis-cli flushall');
            Mage::log('Redis cache flush script ran with message. ' . $output, null, 'atwix_flushcache.log', true);
            Mage::getSingleton('adminhtml/session')->addSuccess('Redis cache flush script ran with message. ' . $output);
        } catch(Exception $e) {
            Mage::log($e->getMessage(), null, 'atwix_flushcache.log', true);
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }

    }
}

After clearing the Magento config cache, you will receive the scheduled Redis cache daily clean up. Modify the string in the shell_exec function if you want to flush only a specific Redis database. For example, if you set the session storage as database “1” in your Magento configuration, then you should replace the line 12 of the Cron.php file with the following data:

$output = shell_exec('redis-cli flushdb 1');

You can also run this process manually, after installing some of the Cron Scheduler extensions (we recommend to use AOE Scheduler). Otherwise, you can add the Model method call to any of the controller action or write a custom one with a single line:

Mage::getModel('atwix_redisflush/cron')->flushRedisCache();

That is all. We always appreciate your feedback, feel free to add your thoughts in the comments below.