Drupal Taxonomy Tree Caching
For a site I am developing, I needed a way to import a set of files in a Drupal system. The files were converted into nodes, each node having (amongst other stuff) a taxonomy term attached (think categories).
First of all: the taxonomy API is not really friendly towards other module developers (or at least, the documentation isn’t). The method for saving a term assumes it is called from the term/add form. No biggie, but not easy if you’re looking for a quick way to save a term (it turns out the method works fine outside the form too).
Anyway: taxonomy_get_tree caches its results per page. That means it’s quite performant to call it several times each page (for the same vocabulary). On the other hand: if you are doing a number of term updates to a vocabulary, those updates aren’t seen by later calls to taxonomy_get_tree, because the data is cached.
This bit me in the ass today: I was creating terms on the fly as I was importing files. Problem was that I saw the same term coming back multiple times, as if my code didn’t find the existing terms. Of course it didn’t: after the first call the “empty” tree was cached and I got stuck with out of date data for the rest of the import procedure.
So here’s a patch to solve that problem: it introduces a way to enable or disable that tree caching. If you disable the cache, the data will still be “remembered” (so once you turn it back on, it doesn’t require another database trip), but it will be reloaded with each call to taxonomy_get_tree(). And if you’re done updating the tree, just turn it (the caching) on again.
Here’s a patch against Drupal 6.3: taxonomy.patch.
Enjoy!
About this entry
You’re currently reading “Drupal Taxonomy Tree Caching,” an entry on Inferis’ Code Dump
- Published:
- 05.09.08 / 1pm
- Category:
- drupal
2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]