Magento invalidated cache can slow down the website

Last week, one of our client’s website which is running on Magento became noticeably slow. Using Chrome developer’s tool it shows the page load took 14+ seconds and even longer. So we started to investigate the issue and we were a little surprised to find out the cause of the slowness.

Luckily we run all our own servers so it didn’t take long to track the problem down.

  • First thing we checked was the server it was hosted on, to see if it was a resource issue. The server load was average and MySQL didn’t seem to have any problems. Also no resources were limited on that domain so we concluded that it wasn’t a resource problem on the account or globally on the server.
  • Next we had a discussion between our developers and confirmed there were programming done on the website. This means this must be caused by the Magento core or some plugins this website is using.
  • At this stage, we suspected maybe the caching was turned off, so we went into cache management to check.
    Magento Cache Management
    Caching was on but we discovered that there was an invalided cache.
    Magento Invalided Cache
  • So we selected the invalided cache and refreshed it.
    Magento Refresh Invalided Cache
  • After this we tested the website again. To our surprise, this resolved the websites’s slow loading issue. Now we need to find a way to detect this and fix it automatically.
  • To do this, we created a file called cache-check.php in the root of the Mangeto directory. In our case, it is the public_html. This file consist of the following codes:<?php
    require ‘app/Mage.php’;
    $invalidCache = Mage::app()->getCacheInstance()->getInvalidatedTypes();
    foreach($invalidCache as $i)
    {
    Mage::app()->getCacheInstance()->cleanType($i[“id”]);
    }
  • Next we added a cron in cpanel to run this every hour. Now the system will check for invalided cache and refresh it every hour. We can set this to a smaller interval if we want, but hourly check should be sufficient.

Hope this may help someone out with a similar problem.