Make WordPress Core

Opened 15 years ago

Closed 9 years ago

#14983 closed enhancement (wontfix)

Get cache object by field

Reported by: johnbillion's profile johnbillion Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Cache API Keywords: has-patch needs-refresh needs-testing
Focuses: Cc:

Description

Functions such as get_term_by() don't check the object cache before performing their query. We should introduce a function for getting a cache object by its field/value pair to make these more efficient.

I've written a simple function, get_cache_object_by(), which accepts $field and $value parameters in the same way get_term_by() does, and an optional $group parameter for the cache group.

get_term_by() and other get_*_by() functions could check the cache with this function before performing their query.

The function is compatible with external object cache plugins that use the cache member variable of the $wp_object_cache object, as they should.

Attachments (1)

get_cache_object_by.php (583 bytes) - added by johnbillion 15 years ago.

Download all attachments as: .zip

Change History (12)

#1 @johnbillion
15 years ago

get_cache_object_by() function. If this looks ok I'll patch it up.

#2 follow-up: @scribu
15 years ago

I was also trying to make get_term_by() check the cache in #13170 but I like your approach better.

Some testing would be required, to see if this actually boosts performance in most cases or not.

#3 in reply to: ↑ 2 @johnbillion
15 years ago

Replying to scribu:

I was also trying to make get_term_by() check the cache in #13170 but I like your approach better.

Some testing would be required, to see if this actually boosts performance in most cases or not.

It only loops over the cache objects for the corresponding group so it should be fairly quick.

My use case was where some tags were manually listed in the footer of a site. The code uses get_term_by() but performs a query even though the terms are already displayed on the site and are in cache. Not sure where and how often get_term_by() is used by default.

#4 @johnbillion
15 years ago

It looks like the only other function that would benefit from get_cache_object_by() would be get_page_by_title(). get_user_by() checks user objects that are cached by field (odd that this behaviour is different to term caching).

get_page_by_path() would benefit but only when $page_path is just a top level slug and not a path separated by slashes.

#5 @scribu
15 years ago

I'm surprised we don't have a {{get_post_by()}}} function.

Anyway, +1 from me.

#6 @markjaquith
14 years ago

  • Milestone changed from Awaiting Review to Future Release

Yes please.

#8 @scribu
14 years ago

From http://core.trac.wordpress.org/ticket/13170#comment:6:

On multisite installs getting a group will return a colossal amount of data. You also can't assume the backend cache will support it. I think the only way we can do this is as we do users, adding a slug to id cache.

So, yes, this still warrants some thorough testing.

#9 @nacin
11 years ago

  • Component changed from Performance to Cache

#10 @chriscct7
10 years ago

  • Keywords has-patch needs-refresh needs-testing added

#11 @boonebgorges
9 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

The cache backends I am familiar with do not have any support for groups. I'm afraid that the current proposal - though great conceptually - is not going to work given this limitation. Field-specific lookups, like in get_term_by(), will always need either (a) their own cache, or (b) to become wrappers for the more generic query, whose cache key is salted with an incrementor.

Note: See TracTickets for help on using tickets.