The idea of the #cache property of a render array is that it will manage caching of an element. It has an 'expire' member, which you would think would mean something. (My assumption is that someone using #cache and setting '#cache['expire'] would want it to expire. However, there's no provision for that in the code.

The attached one-liner adds that in.

Example: Without this patch the following would cache the element, but it would never expire:

  $interval = 10;
  $render_array = array( t('cache demonstration') => array(
      '#markup' => t('The current time was %time when this was cached. Updated every %interval seconds', array('%time' => date('r'), '%interval' => $interval)),
      '#cache' => array(
        'keys' => array('render_example', 'cache', 'demonstration'),
        'bin' => 'cache_page',  // Not probably the best place for this.
        'expire' => time() + $interval,
        'granularity' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE,
      ),
      ),
  );
CommentFileSizeAuthor
drupal.render_cache_invalidate.patch576 bytesrfay

Comments

moshe weitzman’s picture

Isn't it the responsibility of cache_get() to avoid returning expired items?

chx’s picture

Note that system cron forces expired items out of the cache AFAIK

moshe weitzman’s picture

Status: Needs review » Closed (works as designed)

thx chx. i guess rfay had not run cron recently.

we could ask cache_get() to check expired as well but that would be an significant API change for swappache cache backends. Probably D8.

rfay’s picture

chx is right, of course. My patch would be far more predictable. Thanks for taking a look.