Time sensitive data caching

I've been working on this rather large project that must make several REST based web service calls for each page generation. Each call can be cached for a different amount of time, ranging anywhere from 1 minute to 1 hour depending on the method being called. So I set out to make sure that the results of all of these calls were being cached for the appropriate amount of time.

This isn't the first time I've used Drupal's caching mechanism. This is, however the first time I have used it where the data was very time-sensitive. The data that can be cached for 1 minute, MUST expire after 1 minute. No problem, I thought. There is the expire parameter to cache_set() which accepts a UNIX timestamp. It was easy enough to construct these timestamps and implement them.

<?php
  cache_set
($cid, 'cache', serialize($data), time() + $expire);
?>

Now here's the rub. For some reason, I expected these cache entries to be invalidated after the specified time was up. So for an entry that was set with a time + 60 second expire, I would expect to see new results after 60 seconds. It turns out this is not the case.

After looking through the core code, I realized that there is no place where the cache gets invalidated. The only time a cache entry ever gets cleared is through the cache_clear_all function. But I don't want to clear the cache, I just want to expire data that should be expired.

What a misleading function name.

To expire cached data that was set with either CACHE_TEMPORARY or a timestamp as its expires parameter, you must call cache_clear_all() without a cache id and with the table name.

<?php
  cache_clear_all
(NULL, 'cache');
?>

I put this function call in my menu callback function, before trying to retrieve any cache results, and now everything works as expected. Would it be too much trouble to have a convenience function with a better name for this? My suggestion:

<?php
function cache_expire($table) {
 
cache_clear_all(NULL$table);
}
?>

See also:

Posted at 11:50 pm on July 1, 2008 — Tags: cache | drupal | Drupal 5

1 Response so far

A young boy and his dad went out fishing one fine morning. After a few quiet hours out in the boat, the boy became curious about the world around him. He looked up at his dad and asked "How do fish breath under water?"His dad thought about it for a moment, then replied, "I really don't know, son."The boy sat quietly from another moment, then turned back to his dad and asked, "How does our boat float on the water?"Nnce again his dad replied, "Don’t know, son."Pondering his thoughts again, a short while later, the boy asks "Why is the sky blue?"Tiffany Rings Again, his dad replied. "Don’t know, son."The inquisitive boy, worried he was annoying his father, asks this time "Dad, do you mind that I'm asking you all of these questions?""Of course not son." replied his dad, "How else are you ever going to learn anything?"

1
Jun 25, 2010 5:58 am

Post new comment

  • You can use some basic HTML in your posts
  • I will never publish your email address.
  • If you have a Gravatar associated with your email address, it will be used.
  • If you are putting spammy links in your posts, the entire post WILL be deleted.