Make WordPress Core

Opened 23 months ago

Last modified 7 months ago

#20875 new enhancement

Introduce wp_cache_get_multi()

Reported by: nacin Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Cache API Keywords: has-patch early
Focuses: Cc:


Both options (see #10274) and themes (see #20103) could benefit from a cache backend that implements get_multi(). For options, this means we can use individual keys. For both themes and options, we'd be able to make fast multiple gets.

Both APC and Memcached (but not Memcache) implement multiple-get. A fallback would be looping over get().

Separately, as this would be a new function we use in core, we probably need to start doing some kind of versioning for drop-ins like db.php and object-cache.php.

Attachments (3)

20875.diff (3.3 KB) - added by tollmanz 21 months ago.
UT20875.diff (5.1 KB) - added by tollmanz 21 months ago.
20875.2.diff (2.4 KB) - added by wonderboymusic 15 months ago.

Download all attachments as: .zip

Change History (12)

comment:1 tollmanz23 months ago

  • Cc zack@… added

tollmanz21 months ago

tollmanz21 months ago

comment:2 tollmanz21 months ago

  • Cc tollmanz@… added
  • Keywords has-patch added

I imagine that there is a lot to be discussed about how this should work, but I am adding a patch to move things along. I added a first attempt at a get_multi function with this patch. Additionally, I have written unit tests and uploaded them here (would appreciate some direction on if these should also be added to the unit test trac and what's the right way to do that organizationally).

This get_multi function uses the get function to retrieve values from the $cache var. An array of keys and groups are sent to the function. The function attempts to figure out the relationship between the $keys and $groups values. The logic is:

  • If $keys and $groups are both strings, it is assumed to be a simple get.
  • If $keys and $groups are arrays and are the same length, $keys[n] is assumed to be in $groups[n].
  • If there are more $groups than $keys, $keys[n] is assumed to be in $groups[n] until all values in $groups is exhausted. At that point, the remaining $keys values are assumed to belong to the group 'default'.
  • If $keys is an array and $groups is a string or single value array, all $keys are assumed to be in $groups (if string) or $groups[0] (if array).

Values are returned as an array with top level keys being groups. The second level keys are the cache key values. This should match the organization within the $cache var itself.

A number of different scenarios have been tested in the unit tests and this seems to be working well for the assumptions I have laid out.

comment:3 wonderboymusic18 months ago

I cribbed the Memcache code to implement it here: http://wordpress.org/extend/plugins/memcached-redux/

comment:4 aaroncampbell15 months ago

  • Cc aaroncampbell added

comment:5 ryan15 months ago

For back compat reasons, the API here should handle the call signature used in the memcached plugin. Looks like the patch might already handle that. http://plugins.svn.wordpress.org/memcached/trunk/object-cache.php

Last edited 15 months ago by ryan (previous) (diff)

wonderboymusic15 months ago

comment:6 wonderboymusic15 months ago

  • Milestone changed from Awaiting Review to 3.6

I squeezed the code into new code - I don't think the return value needs to be associative and bucketed. I think a standard use-case for get_multi in action is:

list( $one, $two, $three ) = wp_cache_get_multi( array( 'one', 'two', 'three' ), 'numbers' );

comment:7 ryan12 months ago

  • Keywords 3.7-early added
  • Milestone changed from 3.6 to Future Release

comment:8 wonderboymusic9 months ago

  • Milestone changed from Future Release to 3.7

these are all marked 3.7-early

comment:9 nacin7 months ago

  • Keywords early added; 3.7-early removed
  • Milestone changed from 3.7 to Future Release

Holding off on this until ryan can weigh in. We don't have an acute need for it in core.

Note: See TracTickets for help on using tickets.