How to avoid field cache?

Created on 28 November 2018, over 6 years ago
Updated 28 March 2025, about 1 month ago

Hi,

So the cache exclusion was not working for me.
My page was correctly detected, and the module was calling drupal_page_is_cacheable(FALSE); but I believe
1/ it is not enough
2/ it is not called at the right time

I am not expert enough (and have no time) for a generic fix, but I managed to make it work, so I am going to explain here how I did, so that people can fix it on their side if needed, or better, someone can propose a generic solution.

1/ Why the drupal_page_is_cacheable call is not enough
So, checking the cache_field table, I noticed that there was a record for my node here (field:node:902) which seems to be the body of my page (indeed in drupal 7 if I am correct, the body part of a page is stored as field).

So even if the page itself was not cached, the fact that the body field was, made this nearly useless, because I had the same body for every user loading the page which I did not want. (I checked by printing the generation time in comments in the page, and the generation time was never changing, unless I cleared cache entirely, or deleted the cache_field record manually)

Indeed, after searching where the fields were cached, I read the field_attach_load function, from the Drupal Cord filed module.
Guess what, it checks nothing before caching the field. So calling drupal_page_is_cacheable(FALSE); or even $GLOBALS['conf']['cache'] = FALSE; (as I saw in the Uncache module), has no effect.

2/ Why it seems there is a calling order problem
When checking, using dpm in the cacheexclude module and in the field module, I noticed that the field caching seems to be done EVEN BEFORE the cacheexclude function is called... So even if we knew how to solve 1/, it is still needed to figure how to make sure the cacheexclude code is executed first.

What I did
So I figured out that to fix this quickly and with my limited knowledge of the drupal API, what I could do was (and I do no like that) modifying the drupal core field module.

I modified the field_attach_load function so that it replicates the behavior of cacheexclude module check

After the module setting the $cache_write variable, I added a few line to avoid it if needed.

$cache_write = $cache_read && !isset($options['field_id']);
  
// CUSTOM CODE ADDED TO MAKE CACHE EXCLUSION WORK
$pages = trim(variable_get('cacheexclude_list', ''));
if (strlen($pages) && (drupal_match_path($_GET['q'], $pages) ||
                         drupal_match_path(drupal_get_path_alias($_GET['q']), $pages) )) 
{
	$cache_write=false;
}

After uploading this customized module and clearing the whole cache, each refresh on my page listed in the cacheexclude list was printing a new generation time.

Please let me know what you think about the issue, if you think I am stupid or if it needs a fix (in cacheexclude, drupal core or both)

Cheers,
Alex

πŸ’¬ Support request
Status

Closed: outdated

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024