Posted by robertDouglass on July 29, 2007 at 4:48pm
| Project: | Drupal core |
| Version: | 7.x-dev |
| Component: | taxonomy.module |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
The not-so-often used taxonomy_get_synonym_root needs static caching. When it does get used, it suffers from duplicate queries.
<?php
function taxonomy_get_synonym_root($synonym) {
- return db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym));
+ static $synonyms = array();
+ if (empty($synonyms[$synonym])) {
+ $synonyms[$synonym] = db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym));
+ }
+ return $synonyms[$synonym];
}
?>| Attachment | Size | Status | Test result | Operations |
|---|---|---|---|---|
| taxonomy-synonym-cache.patch | 950 bytes | Ignored: Check issue status. | None | None |
Comments
#1
db_fetch_object returns a 0... so I cast it to an object so it gets cached. Other ideas are welcome.
<?phpfunction taxonomy_get_synonym_root($synonym) {
- return db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym));
+ static $synonyms = array();
+ if ($synonym == '') {
+ return NULL;
+ }
+ if (empty($synonyms[$synonym])) {
+ $synonyms[$synonym] = (object)db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym));
+ }
+ return $synonyms[$synonym];
}
?>
#2
This also still applies, with offset.
#3
There are no examples of this being used in contrib, but I ran into the caching issue with a client who has a proprietary module depending on this function. Rerolled patch to apply without offsets.
#4
This still works against HEAD.
#5
Still applies with offset of 120 lines
#6
Bumping to D7, +1, needs a re-roll for dbtng since db_fetch_object is deprecated.
#7
Patch doesn't actually touch the query, so this could be done with a wider conversion. Attached patch adds a reset parameter and some doc.
#8
grr
#9
Fixed the documentation, converted the query to PDO, removed the ugly cast to object.
#10
Postponing for #254491: Standardize static caching to commit.
#11
Please don't postpone little patches like this one in benefit of that kitten-killer. webchick groans about committing small patches, but in fact we know that she loves that.
#12
Still applies cleanly, changes are good. RTBC.
#13
Hm? I never groan about small patches. :D I groan about adding more $reset params to functions, but I agree that that other issue is going to take some time to sort out properly, and this function can benefit while we wait.
Committed with a couple small changes to the line spacing because I'm just that way. ;) Looking forward to seeing some or all of you in #254491: Standardize static caching so we can fix this properly.
#14
Automatically closed -- issue fixed for two weeks with no activity.