Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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;
}
Comment | File | Size | Author |
---|---|---|---|
#7 | taxonomy_menu-cache_count-1887782.patch | 1.11 KB | makbay |
#3 | taxonomy_menu-cache_count-1887782-3.patch | 1.21 KB | jenlampton |
Comments
Comment #1
hles CreditAttribution: hles commentedYes 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 !
Comment #2
kirie CreditAttribution: kirie commentedCool - I have a bit much on my plate right now, but I'll try to provide that patch!
Comment #3
jenlamptonHere'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.
Comment #5
jenlamptonFunny, looks like tests are running on the wrong branch?
Comment #6
makbay CreditAttribution: makbay commentedHi, this is a quite terrible issue and makes needless hundreds of queries, slows down the page loading. I confirm the following patch is working.
Comment #7
makbay CreditAttribution: makbay commentedComment #8
makbay CreditAttribution: makbay commentedComment #10
dstolThank you!
Comment #12
DamienMcKenna