[wp-trac] [WordPress Trac] #21760: get_term_by() calls are not cached

WordPress Trac wp-trac at lists.automattic.com
Tue Sep 4 16:28:22 UTC 2012


#21760: get_term_by() calls are not cached
----------------------------------------+------------------------------
 Reporter:  wonderboymusic              |       Owner:
     Type:  enhancement                 |      Status:  new
 Priority:  normal                      |   Milestone:  Awaiting Review
Component:  Taxonomy                    |     Version:  2.3
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |
----------------------------------------+------------------------------
Description changed by wonderboymusic:

Old description:

> {{{get_term()}}} is the simplest way to retrieve one term, but it
> requires {{{term_id}}} and {{{taxonomy}}}. Because of this, terms are
> cached with {{{term_id}}} as key and {{{$taxonomy}}} as bucket. As a
> result, you can't easily grab a term by slug, unless you use
> {{{get_term_by( 'slug' )}}}. {{{get_term_by( 'slug' )}}} and
> {{{get_term_by( 'name' )}}} don't even have a query cache, so they go to
> the database every time. Because you can't get a term by {{{slug}}}
> without hitting the db, every place you want to get a term by {{{slug}}}:
> you first have to transform it into a {{{term_id}}} where it will then be
> cached. This is inefficient because the user may query by {{{slug}}}
> constantly and never by {{{term_id}}}.
>
> My patch does the following:
>
> 1) Changes the type of argument 1 of {{{get_term}}} to be mixed -
> {{{int}}}, {{{string}}}, or {{{object}}}[[BR]]
> 2) Allows key in the {{{$taxonomy}}} cache bucket to be {{{name}}},
> {{{slug}}}, or {{{term_id}}}[[BR]]
> 3) Deprecates {{{get_term_by()}}}[[BR]]
> 4) updates {{{clean_term_cache()}}} to also evict by {{{name}}} and
> {{{slug}}}
>
> The result is that {{{get_term()}}} can be called with {{{term_id}}},
> {{{slug}}}, or {{{name}}} - and then CACHED by {{{term_id}}}, {{{slug}}},
> or {{{name}}}.

New description:

 {{{get_term()}}} is the simplest way to retrieve one term, but it requires
 {{{term_id}}} and {{{taxonomy}}}. Because of this, terms are cached with
 {{{term_id}}} as key and {{{$taxonomy}}} as bucket. As a result, you can't
 easily grab a term by slug, unless you use {{{get_term_by( 'slug' )}}}.
 {{{get_term_by( 'slug' )}}} and {{{get_term_by( 'name' )}}} don't even
 have a query cache, so they go to the database every time. Because you
 can't get a term by {{{slug}}} without hitting the db, every place you
 want to get a term by {{{slug}}}: you first have to transform it into a
 {{{term_id}}} where it will then be cached. This is inefficient because
 the user may query by {{{slug}}} constantly and never by {{{term_id}}}.

--

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21760#comment:11>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list