Hello and thanks for a great module!

When I was using devel to investigate the number of db queries I noticed that there would be several hundred queries generated by the _taxonomy_menu_term_count function. Looking through the issues I found this old post http://drupal.org/node/1355260 and simply added the caching code to the _taxonomy_menu_term_count function - and it seems to be working well!

Would you consider adding this (or similar) functionality to the module?

Here is what my code looks like if anybody is interested in trying it out for themselves:

function _taxonomy_menu_term_count($tid) {
  // Construct a cache ID
  $cid = 'taxonomy_menu:term_count:' . $tid;

  // Try to get the item from the cache
  $cache = cache_get($cid);
  if ($cache) {
    // If we got it from the cache, return that.
    return $cache->data;
  }

  // It is not in the cache, so we issue a query
  $result = db_select('taxonomy_index', 'tn');
  $result->condition('tid', $tid);
  $result->join('node', 'n', 'n.nid = tn.nid AND n.status = 1');
  $result->addExpression('COUNT(n.nid)', 'term_count');
  $temp = $result->execute();
  $temp = $temp->fetchObject();
  $data = $temp->term_count;

  // We store it in the cache
  cache_set($cid, $data, 'cache', REQUEST_TIME + 1215);
  // We return the result
  return $data;
}

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hles’s picture

Title: Implement caching for » Implement caching for node count
Version: 7.x-1.4 » 7.x-2.x-dev
Category: feature » bug

Yes we definitely have to do that. I already took a look a that a few times and used drupal_static instead, which is only available in Drupal 7. If you want to provide a patch, please do so against the 7.x-2.x branch, that would be great !

kirie’s picture

Cool - I have a bit much on my plate right now, but I'll try to provide that patch!

jenlampton’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev
Issue summary: View changes
Status: Active » Needs review
FileSize
1.21 KB

Here's a patch that adds the caching as recommended. It is against the 1.x branch since it looks like not much is going on in 2.x anymore. Please let me know if you would prefer that patches are still rolled against 2.x.

Status: Needs review » Needs work

The last submitted patch, 3: taxonomy_menu-cache_count-1887782-3.patch, failed testing.

jenlampton’s picture

Status: Needs work » Needs review

Funny, looks like tests are running on the wrong branch?

makbay’s picture

Hi, this is a quite terrible issue and makes needless hundreds of queries, slows down the page loading. I confirm the following patch is working.

makbay’s picture

makbay’s picture

Status: Needs review » Reviewed & tested by the community

  • dstol committed 5a0fcd1 on 7.x-1.x authored by makbay
    Issue #1887782 by makbay, jenlampton: Implement caching for node count
    
dstol’s picture

Status: Reviewed & tested by the community » Fixed

Thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

DamienMcKenna’s picture