1

Using the steps explained here: http://www.lullabot.com/blog/article/beginners-guide-caching-data-drupal-7 I'm storing an array with specified expiry time.

But what is happening is that even after expiry of time, the cache_get is returning the stale cache. Even Cron run is not deleting the cache entry(I've checked the cache_block table).

This is the code:

     $out = &drupal_static(__FUNCTION__);

        if ( !isset($out)) {
            if ( ( $cache = cache_get(__FUNCTION__,'cache_block'))) {
                $out = $cache->data;
                dsm("cache returned");
            } else {

//do something
//...........
  $out = '<ul style="list-style:none;padding-right:20px">' . $out . "</ul>";
            cache_set(__FUNCTION__, $out, 'cache_block', strtotime("23:59:59")+2);
        }//else ends
    }//!isset ends

    return $out;
}//function ends

Only cache clear all in the Performance tab is clearing this cache entry in the cache table.

I'm confused how this expired cache will be deleted.

In that article I can read this statement:

Expired entries will be automatically discarded as they pass that date.

But does not seem to be happening. Also I've verified that "expire" value in the cache table is beyond current time.

I can clear the cache myself but then cache_get function does not return the "expire" time in the table.

Anything I'm missing here?

2
  • Please show us your code to store cache data. We can't tell what's wrong with it if we can't see it.
    – Mołot
    Oct 17, 2013 at 8:35
  • I've edited it.
    – user5858
    Oct 17, 2013 at 9:17

1 Answer 1

0

As you can see in the documentation:

$expire: One of the following values:

  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.
  • CACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.
  • A Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

Please note - it's not "at most". It's "at least". So your code works just the way it is to be expected. If you want to force-expire it, use cache_clear_all( __FUNCTION__ , 'cache_block'), for example in hook_cron() or at the beginning of your code.

Also note that it looks like you are re-saving cache at each use. That's not an usual thing, saving data is CPU and HDD expensive. It costs more than not caching at all. So consider only setting cache if it was not found in the first place, and needs to be regenerated. If cache is already set, let it expire and don't set it again.

2
  • The +2 seconds is for testing. I've placed the correct code now. How would I know when the cache has expired?
    – user5858
    Oct 17, 2013 at 9:54
  • It's not about +2, really. It's about setting cache at use, no matter if it was there already or not.
    – Mołot
    Oct 17, 2013 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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