The forum active block shows the latest active forum threads. It uses caching in the render element, and caches a query using this mechanism. This means that the latest forum threads won't show directly. I don't see why this caching is necessary, because the forum_index table is searched on the 'last_comment_timestamp'. This column isn't indexed, but if you index it the query is so damn fast that caching it is nonesense.
Our customer does not accept having to wait 24 hours or so before the block is updated and I can't imagine a lot of situations where this is acceptable. So I tried writing a hook to expire the cache directly using:
/**
* Implements hook_block_view_forum_active_alter().
*/
function sgh_block_view_forum_active_alter(&$data, $block) {
//Short cache; refresh always
$now = (int) date('U');
$data['content']['#cache']['expire'] = $now;
}
It didn't have any effect. I went through the source code, reviewed both drupal_render, drupal_render_cache_get and cache_get and noticed a serious problem. At no place at all it is checked whether the cache is expired. Because the forum module uses render caching, this means that setting a different expire does not have any effect and expired items will always be shown, unless you manually clear the cache.
In fact, I find it strange that cache_get returns expired caches. But if this is a decision by design, there is no other alternative than to actually return FALSE from drupal_render_cache_get when the cache is expired and let the forum module generate the content again. This does not happen now.
I propose the following change in drupal_render_cache_get: add the line
if ($cache->expire > time()) return FALSE;
immediately after the line
if (!empty($cid) && $cache = cache_get($cid, $bin)) {
Regards,
Bas
Comments
Comment #1
damien tournoud commentedYou can just strip the whole
#cacheif you don't like it.So, there are two issues here:
'bin'so that it ends up in thecache_blockbin (which is cleared automatically when content is saved)drupal_render_cache_get()should check if cached items are expiredComment #2
bvanmeurs commentedHi Damien,
Thanks. I disabled the cache and it works fine now, but in fact the problem remains. Good to see that it's on the task list for Drupal 8!
Comment #3
charlie-s commentedThe expiration check isn't a D7 bug? If not, is the intent of the 'expire' property in render arrays simply for developers to employ as they see fit?
Comment #4
marcingy commentedLooking at D8 the database back does the following in protected function prepareItem($cache, $allow_invalid)
And similar happens in d7 so the check is happening just at different point in the stack.
Comment #4.0
marcingy commentedAdd a solution