WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 6 months ago

#14983 new enhancement

Get cache object by field

Reported by: johnbillion Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Cache API Keywords:
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 4 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 johnbillion4 years ago

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

comment:2 follow-up: scribu4 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.

comment:3 in reply to: ↑ 2 johnbillion4 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.

comment:4 johnbillion4 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.

comment:5 scribu4 years ago

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

Anyway, +1 from me.

comment:6 markjaquith3 years ago

  • Milestone changed from Awaiting Review to Future Release

Yes please.

comment:8 scribu3 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.

comment:9 nacin6 months ago

  • Component changed from Performance to Cache
Note: See TracTickets for help on using tickets.