2

I have a function:

function _dc_election_races_get_articles($search_term)
{
    $search_url = 'http://feeds.example.com/vote_up/search.php?search='.rawurlencode($search_term);

    if ($cached = cache_get($search_url, 'cache'))
    {
        $results = $cached->data;
    }
    else
    {
        $results = json_decode(file_get_contents($search_url));
        cache_set($search_url, $results, 'cache', 60*60*3); //3 hours
    }

    return $results;
}

I basically want the cache to expire after 3 hours and re-fetch the data from the url. The documentation on drupal made it seem like that it might not be removed from cache after 3 hours. Will cron cleanup caches?

3

If you start chasing the code in cache_get you will eventually end up in DrupalDatabaseCache::getMultiple. If you look at the code, you will see a call to DrupalDatabaseCache::garbageCollection at the top. This will handle cache expiration before cron runs.

As part of system_cron you will see that cache_clear_all get called with NULL as the first parameter which

$cid: If set, the cache ID to delete. Otherwise, all cache entries that can expire are deleted.

This cleans up the cache tables every time cron runs.

The Drupal 6 plumbing is different for pruning the cache (start reading cache_get, but the end result is essentially the same.

TL;DR, your cache TTL will always be honored when you get values, it's just the cache table cleanup that may not happen exactly when the TTL is up.

| improve this answer | |
  • The cache is NOT expiring after 3 hours. What else could be causing this? – Chris Muench Sep 20 '12 at 12:05
  • Try cache_set($search_url, $results, 'cache', time() + 60*60*3); //3 hours from now – mpdonadio Sep 20 '12 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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