19

In the Magento admin under Cache Management, what does it mean when it shows a cache as invalidated? How does Magento know a cache is invalidated? In particular, I'm wondering about HTML Block cache. What conditions would cause this cache to show up as invalidated?

localfilmmaker
  • 416
  • 1
  • 4
  • 9

4 Answers4

15

In Magento, whenever you make changes to products, static blocks, etc, it recognizes that the data in the database is no longer the same as what it has in the cache. Unfortunately, Magento doesn't realize what cache data is different, just that something is different.

You will need to go into System > Cache Management and refresh the invalidated cache types.

EDIT:

Create a module (or use an existing module) that you can use to set up a cron job for refreshing the cache. Create a file: {namespace}/{modulename}/Model/Observer.php

Inside that file:

<?php
class <namespace>_<modulename>_Model_Observer {

  public function refreshCache() {
    try {
      $allTypes = Mage::app()->useCache();
      foreach($allTypes as $type => $blah) {
        Mage::app()->getCacheInstance()->cleanType($type);
      }
    } catch (Exception $e) {
      // do something
      error_log($e->getMessage());
    }
  }

}

In your module's etc/config.xml:

<config>
  ...
  <crontab>
    <jobs>
      <{modulename}_refresh_cache>
        <schedule><cron_expr>* * * * *</cron_expr></schedule>
        <run><model>{modulename}/observer::refreshCache</model></run>
      </{modulename}_refresh_cache>
    </jobs>
  </crontab>
  ...
</config>

Now as long as cron is configured correctly on your server, the cache will update automatically, as often as cron runs.

Magento Guy
  • 2,493
  • 1
  • 16
  • 13
  • 4
    I understand how to refresh the cache, which I do when I see that it has been invalidated. I just don't really understand how it knows that the cache has been invalidated. And related- why doesn't it automatically refresh the cache if it knows it is invalid? Is there any risk to refreshing the cache? – localfilmmaker Dec 08 '11 at 16:46
  • Whenever you make changes, magento fires events. There are listeners to these events that invalidate the relevant cache. As for why it does this (and why it doesn't automatically refresh) this is ultimately a design decision, but probably has something to do with being able to stage content. For example, you could make changes to several products that all relate to one another, and can then refresh the cache. I've overcome this with a cron job that runs every time cron runs on the server, and calls a function to refresh the cache automatically. I'll edit my original answer to include the code. – Magento Guy Dec 08 '11 at 23:26
  • 3
    Very bad solution: setting a cron that refresh all cache, everytime it runs, it is like disabling the cache ... you should at least check if the cache is invalidated and set the cron every 10/30 min ... – WonderLand Oct 08 '15 at 10:03
  • I disagree @WonderLand. Like many out there, my client can enter & manage products. Adding system tasks to rebuild indexes & flush cache storage is way beyond his "pay grade". He's not technical. All he wants is to manage products. He relies on me to manage the site. I'm not in the backend watching him update products nor would I ask him to email me each time he updates just so I can flush cache. I am very glad I have him as a client. He's skilled in mfg products. I'm skilled in web techs. We'll keep it that way. – H. Ferrence May 05 '16 at 18:45
  • 1
    The correct solution is Magento needs to auto rebuild the cache files anytime a product change is made as part of its core system. This way we don't have to manually participate in the process or write scheduler jobs to handle this in the event it might be needed. Just seems Magento is 1-step short in the product update process. – H. Ferrence May 05 '16 at 18:49
  • 2
    Your answer is correct, but your fix is completely wrong. Removing all the content of the cache every 5 minutes makes the website even slower than not activating any cache at all. See the answer of @WagnerBertoliniJunior for a pertinent workaround. – OddBrew Nov 30 '18 at 17:02
3

None of the answers so far provided a correct definition, neither a solution. So I decided to necro-answer :-)

what does it mean when it shows a cache as invalidated?

It means that the cached data does not reflect the actual data from the database. As simple as that.

Now, the important question which wasn't asked is what makes invalidation happen. There are basically two major reasons for this:

  • You have a bad code that uses direct SQL queries against the database - this bypasses Magento classes, so it can't know which data is correctly indexed, thus it has to mark entire indexes/caches as invalid.
  • Reindexing isn't running. This means that Magento cron is likely not set up. The reindexing is what triggers individual cache entries to be updated. Another sub-reason is that reindexing is set to "On save", then the page was timed out / closed prematurely while saving a product / CMS page, etc.
  • The least likely one (but still valid) is a Magento bug or some major settings change that requires a cache to be cleaned in its entirety.

Solutions are:

  • Get rid of any plugins (or developers!) which introduce direct SQL queries to your Magento codebase
  • Ensure Magento cron is configured and running, ensure that indexes are set to be updated "On Schedule"

A solution like "clean cache which was invalidated" is absolutely gross. That is addressing the symptom instead of the root cause. This comes at the cost of many lost conversions (read = money) because you're essentially running cacheless - for even average stores it may take days to fully recover from a single cache clean and warm up the cache to its full potential.

Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
0

2015: AOE Sheduler is able to clean the cache by cronjob. Change JOB

"core_clean_cache"

from 30 2 * * * (Daily 2:30) to 59 * * * * (Every 59 Minutes).

TonkBerlin
  • 191
  • 1
  • 6
0

The @Magento Guy answer is correct, but I think this solution below can help you on refreshing just the invalidated caches on Magento.

I use Bitnami Magento Stack, for me this solution below was the best I have found.

I've tried to create a Mage_Shell_Class php file, but without success (invalid cache array were always empty when it runs, no matter what, and I really don't imagine why).

I've created a php file 'sample.php':

<?php

require 'app/Mage.php';

$invalid = Mage::app()->getCacheInstance()->getInvalidatedTypes();

foreach($invalid as $i)
{
    Mage::app()->getCacheInstance()->cleanType($i["id"]);
}

I've placed it on magento root folder, and to start it I use a cronjob that runs under root user.

So, to create the cronjob on the root user:

sudo crontab -u root -e

And this was my command line to run it:

* * * * * . /opt/bitnami/scripts/setenv.sh ; /opt/bitnami/php/bin/php /opt/bitnami/apps/magento/htdocs/sample.php >> /var/log/cron/cron.log 2>&1

Some parts on this line are very particular to my problem:

  1. Since it just refreshes the invalidated caches I've decided to run it every minute.
  2. setenv.sh is a script that helps me to set the environment when dealing with this particular bitnami stack.
  3. To get the output of this script I used this last part ">> /var/log/cron/cron.log 2>&1" to output errors to a directory that I've created (/var/log/cron), and has given the correct permissions on it.

Probably you need to change the cron line command, but I think this will help you.