I've long wanted to make blocks easier cachable. The attached patch outlines some ideas I have about this. The patch isn't 100% complete yet, but should work if you only use forum.module blocks not blog blocks or statistics blocks.

Comments

killes@www.drop.org’s picture

StatusFileSize
new5.69 KB

Dries pointed out that people might use query rewriting without doing node access stuff. He also wanted to deled the "active forums" block cache when new comments were posted.

killes@www.drop.org’s picture

Title: Better block caching » Better caching
StatusFileSize
new9.02 KB

No reason to limit this to blocks...

merlinofchaos’s picture

In general I +1 this patch. I need to actually test it; hopefully I'll be able to do that later this weekend.

pwolanin’s picture

I haven't dug into the drupal caching mechanism yet, but to me this patch looks like a lot of special cases have to be put in by hand for each block. Could this go into more of an API (even if it's a little less efficient) managed by the blocks module? I.e. in hook_block you flag certain of your blocks as cachable, and either call cache_clear_all('block:forum:', TRUE), or have a block-specific wrapper for this sort of call?

killes@www.drop.org’s picture

Doing this in the block module will be very hard. The problem that our cache system has is that it simply clears the cache based on some rather general assumptions and as a result does it too often. By doing the caching inside the functions which generate the cached content we knoe exactly what is going on and can build caches in a much better way.

Also, this patch is no longer restricted to blocks.

pwolanin’s picture

Any new changes to this patch? I'm thinking about how to make patter use of the chaching mechanism in regards to teh forum navigation elements: http://drupal.org/node/70578

killes@www.drop.org’s picture

StatusFileSize
new9.04 KB

Well, I guess the patch could use some testing. Here is a version for 4.7.

gerhard killesreiter’s picture

StatusFileSize
new12.11 KB

This patch includes caching for the navigation elements as suggested.

gerhard killesreiter’s picture

StatusFileSize
new12.06 KB

was buggy, we need a "per node" cache.

pwolanin’s picture

Ok, I'll try it out.

One quick comment regarding the node_access() cehck. Could this cause some problems, since an access control module that is disabled may leave lots of entries in node_accss?

Also, I will make a patch ASAP for HEAD (and 4.7?) to get the forum topic navigation code out of the theme function and into either a public helper function or maybe into forum_load().

gerhard killesreiter’s picture

if a node_access module leaves stale data in the node_access table we should get the author to clean up the mess. ;)

I am running the latest patch at drupal.org and the forum_get_navigation stuff is run much less often now, roughly only half of the time.

pwolanin’s picture

Ok, well I think we'll have to have words with several of them then ;-)

I just tested with taxnomy access contol, and going through the deactivation procedure still leaves all the data in the node_access table. This doesn't really makes sense, since if you truncate the table and put the default row back in, when you re-activiate the module it recreates the data.

I'll file an issue vs. TAC. I think maybe OG has this issue as well.

gerhard killesreiter’s picture

StatusFileSize
new12.18 KB

the last patch was still buggy, we need to invalidatethe cache if we get new comments.

gerhard killesreiter’s picture

StatusFileSize
new12.29 KB

still buggy, why is $comment once an object and an array sometimes else?

pwolanin’s picture

I'm working on pulling db query and cache code out of the theme_forum_topic_navigation function.

also, forum_admin_configure() needs to have a submit function defined that will expire the cached navigation items if the ordering is changed.

I'll take a stab at that too.

pwolanin’s picture

StatusFileSize
new12.47 KB

Oh wait, I see now. The md5 on the $sql as part of the cache ID alleviates the need to clear the cache when the order is changed.

The attached patch (for 4.7) moves the logic for finding the prev/next forum topics out of the theme function.

Upside- this is now theme independent. A site with multiple themes and different overrides per theme for this function can now use the same cached values via the new helper function.

Downside- added calls to serialize/unserialize.

pwolanin’s picture

StatusFileSize
new12.65 KB

I think maybe a case for clearing the cache is missing in forum_update(). What happens if the forum topic is edited and moved to a different forum?

The answer- with this code everything is broken.

I'm not sure how to deal with this. My inital thought was to check against the forum category returned by node_load(), but this is rather hackish since it relies on that data being stale and still in the static variable.

Alternatives? Maybe have to query the {forum} table?

Attached patch fixes this in the crudest possible way- invaliding all the cached values.

pwolanin’s picture

found a separate bug in function forum_submit(&$node).

If I choose "leave a shadow copy", any changes to the taxonomy *except* the forum category will be discarded. New issue: http://drupal.org/node/76339

pwolanin’s picture

Please review this patch:

http://drupal.org/node/76339#comment-119894

I've structured the bug fix in part to make it easy to have the old and new taxonomy term IDs available in forum_submit for the purpose of selectively clearing the cache. Thus, I'd like to see this sort of patch applied first before working more on this caching patch.

beginner’s picture

subscribe. will test later

beginner’s picture

Peter, I have reviewed the issue you mention. Does this mean that you'll re-roll a new patch here once the other one is committed?

pwolanin’s picture

Yes, I will certainly be willing to work on this patch more when the forum patch is committed, and assuming Gerhard is still interested in following this avenue. Also, this patch will have to be changed some if splitting the cache table goes ahead: http://drupal.org/node/72290

pwolanin’s picture

@killes- please review this related patch: http://drupal.org/node/76339#comment-119894
It was already committed to HEAD.

pwolanin’s picture

StatusFileSize
new12.92 KB

Ok, since the above-referenced patch for forum.module is in, attached is a patch that does the cache invalidation in forum_submit().

The only other place where it might be worth adding some caching logic would be in function forum_get_topics for the case $user->uid=0.

pwolanin’s picture

StatusFileSize
new13.16 KB

oops- forget to remove a line of debugging code. Use attached patch (for Drupal 4.7) instead.

moshe weitzman’s picture

fyi, there is now a node_access rebuild button where a site adminh can clean his table after disabling the last node_access module. actually, the button is safe to use anytime.

pwolanin’s picture

Status: Needs review » Needs work
 function forum_delete(&$node) {
   db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
+  cache_clear_all('block:forum:', TRUE);
+  cache_clear_all('result:forum:forum_get_forums:0:', TRUE);
+  cache_clear_all("result:forum:forum_get_forums:1:$node->tid:", TRUE);
+  cache_clear_all("result:forum:forum_topic_navigation:$node->tid:$node->nid:", TRUE);

Looking through the patch, the last line above should probably be:

+  cache_clear_all("result:forum:forum_topic_navigation:$node->tid:", TRUE);
killes@www.drop.org’s picture

Status: Needs work » Closed (fixed)

I am closing this one on favour of a new, improved approach, see: http://drupal.org/node/80951