diff --git a/tests/phpunit/includes/object-cache.php b/tests/phpunit/includes/object-cache.php index ef03546892037..92c4d75f9e23d 100644 --- a/tests/phpunit/includes/object-cache.php +++ b/tests/phpunit/includes/object-cache.php @@ -987,6 +987,8 @@ public function __construct( $persistent_id = null ) { * @return bool True on success, false on failure. */ public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_add', $key, $value, $group, $expiration, $server_key, $by_key ); + /* * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add @@ -1016,8 +1018,10 @@ public function add( $key, $value, $group = 'default', $expiration = 0, $server_ // Save to Memcached. if ( $by_key ) { + do_action( 'wp_memcached_external_add_by_key', $server_key, $derived_key, $value, $expiration ); $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); } else { + do_action( 'wp_memcached_external_add', $derived_key, $value, $expiration ); $result = $this->m->add( $derived_key, $value, $expiration ); } @@ -1047,6 +1051,7 @@ public function add( $key, $value, $group = 'default', $expiration = 0, $server_ * @return bool True on success, false on failure. */ public function addByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) { + do_action( 'wp_memcached_add_by_key', $server_key, $key, $value, $group, $expiration ); return $this->add( $key, $value, $group, $expiration, $server_key, true ); } @@ -1061,6 +1066,7 @@ public function addByKey( $server_key, $key, $value, $group = 'default', $expira * true on success, or false if cache key and group already exist. */ public function addMultiple( array $items, $group = '', $expiration = 0 ) { + do_action( 'wp_memcached_add_multiple', $items, $group, $expiration ); $values = array(); foreach ( $items as $key => $value ) { @@ -1082,10 +1088,12 @@ public function addMultiple( array $items, $group = '', $expiration = 0 ) { * @return bool True on success, false on failure. */ public function addServer( $host, $port, $weight = 0 ) { + do_action( 'wp_memcached_add_server', $host, $port, $weight ); $host = is_string( $host ) ? $host : '127.0.0.1'; $port = is_numeric( $port ) && $port > 0 ? $port : 11211; $weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1; + do_action( 'wp_memcached_external_add_server', $host, $port, $weight ); return $this->m->addServer( $host, $port, $weight ); } @@ -1101,10 +1109,12 @@ public function addServer( $host, $port, $weight = 0 ) { * @return bool True on success, false on failure. */ public function addServers( $servers ) { + do_action( 'wp_memcached_add_servers', $servers ); if ( ! is_object( $this->m ) ) { return false; } + do_action( 'wp_memcached_external_add_servers', $servers ); return $this->m->addServers( $servers ); } @@ -1128,6 +1138,7 @@ public function addServers( $servers ) { * @return bool True on success, false on failure. */ public function append( $key, $value, $group = 'default', $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_append', $key, $value, $group, $server_key, $by_key ); if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) { return false; } @@ -1147,8 +1158,10 @@ public function append( $key, $value, $group = 'default', $server_key = '', $by_ // Append to Memcached value. if ( $by_key ) { + do_action( 'wp_memcached_external_append_by_key', $server_key, $derived_key, $value ); $result = $this->m->appendByKey( $server_key, $derived_key, $value ); } else { + do_action( 'wp_memcached_external_append', $derived_key, $value ); $result = $this->m->append( $derived_key, $value ); } @@ -1180,6 +1193,7 @@ public function append( $key, $value, $group = 'default', $server_key = '', $by_ * @return bool True on success, false on failure. */ public function appendByKey( $server_key, $key, $value, $group = 'default' ) { + do_action( 'wp_memcached_append_by_key', $server_key, $key, $value, $group ); return $this->append( $key, $value, $group, $server_key, true ); } @@ -1201,6 +1215,7 @@ public function appendByKey( $server_key, $key, $value, $group = 'default' ) { * @return bool True on success, false on failure. */ public function cas( $cas_token, $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_cas', $cas_token, $key, $value, $group, $expiration, $server_key, $by_key ); $derived_key = $this->buildKey( $key, $group ); /** @@ -1217,8 +1232,10 @@ public function cas( $cas_token, $key, $value, $group = 'default', $expiration = // Save to Memcached. if ( $by_key ) { + do_action( 'wp_memcached_external_cas_by_key', $cas_token, $server_key, $derived_key, $value, $expiration ); $result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration ); } else { + do_action( 'wp_memcached_external_cas', $cas_token, $derived_key, $value, $expiration ); $result = $this->m->cas( $cas_token, $derived_key, $value, $expiration ); } @@ -1247,6 +1264,7 @@ public function cas( $cas_token, $key, $value, $group = 'default', $expiration = * @return bool True on success, false on failure. */ public function casByKey( $cas_token, $server_key, $key, $value, $group = 'default', $expiration = 0 ) { + do_action( 'wp_memcached_cas_by_key', $cas_token, $server_key, $key, $value, $group, $expiration ); return $this->cas( $cas_token, $key, $value, $group, $expiration, $server_key, true ); } @@ -1261,6 +1279,7 @@ public function casByKey( $cas_token, $server_key, $key, $value, $group = 'defau * @return int|bool Item's new value on success, false on failure. */ public function decrement( $key, $offset = 1, $group = 'default' ) { + do_action( 'wp_memcached_decrement', $key, $offset, $group ); $derived_key = $this->buildKey( $key, $group ); // Decrement values in no_mc_groups. @@ -1287,6 +1306,7 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { } } + do_action( 'wp_memcached_external_decrement', $derived_key, $offset ); $result = $this->m->decrement( $derived_key, $offset ); if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { @@ -1309,6 +1329,7 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { * @return int|bool Item's new value on success, false on failure. */ public function decr( $key, $offset = 1, $group = 'default' ) { + do_action( 'wp_memcached_decr', $key, $offset, $group ); return $this->decrement( $key, $offset, $group ); } @@ -1330,6 +1351,7 @@ public function decr( $key, $offset = 1, $group = 'default' ) { * @return bool True on success, false on failure. */ public function delete( $key, $group = 'default', $time = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_delete', $key, $group, $time, $server_key, $by_key ); $derived_key = $this->buildKey( $key, $group ); // Remove from no_mc_groups array. @@ -1342,8 +1364,10 @@ public function delete( $key, $group = 'default', $time = 0, $server_key = '', $ } if ( $by_key ) { + do_action( 'wp_memcached_external_delete_by_key', $server_key, $derived_key, $time ); $result = $this->m->deleteByKey( $server_key, $derived_key, $time ); } else { + do_action( 'wp_memcached_external_delete', $derived_key, $time ); $result = $this->m->delete( $derived_key, $time ); } @@ -1371,6 +1395,7 @@ public function delete( $key, $group = 'default', $time = 0, $server_key = '', $ * @return bool True on success, false on failure. */ public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) { + do_action( 'wp_memcached_delete_by_key', $server_key, $key, $group, $time ); return $this->delete( $key, $group, $time, $server_key, true ); } @@ -1383,6 +1408,7 @@ public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) * true on success, or false if the contents were not deleted. */ public function deleteMultiple( $keys, $group ) { + do_action( 'wp_memcached_delete_multiple', $keys, $group ); $values = array(); foreach ( $keys as $key ) { @@ -1400,6 +1426,8 @@ public function deleteMultiple( $keys, $group ) { * @return array|false The next result on success, false on failure. */ public function fetch() { + do_action( 'wp_memcached_fetch' ); + do_action( 'wp_memcached_external_fetch' ); return $this->m->fetch(); } @@ -1411,6 +1439,8 @@ public function fetch() { * @return array|false The results on success, false on failure. */ public function fetchAll() { + do_action( 'wp_memcached_fetch_all' ); + do_action( 'wp_memcached_external_fetch_all' ); return $this->m->fetchAll(); } @@ -1423,6 +1453,8 @@ public function fetchAll() { * @return bool True on success, false on failure. */ public function flush( $delay = 0 ) { + do_action( 'wp_memcached_flush', $delay ); + do_action( 'wp_memcached_external_flush', $delay ); $result = $this->m->flush( $delay ); // Only reset the runtime cache if memcached was properly flushed. @@ -1439,6 +1471,7 @@ public function flush( $delay = 0 ) { * @return bool Always returns true. */ public function flush_runtime() { + do_action( 'wp_memcached_flush_runtime' ); $this->cache = array(); return true; @@ -1470,6 +1503,7 @@ public function flush_runtime() { * @return bool|mixed Cached object value. */ public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $by_key = false, $cache_cb = null, &$cas_token = null ) { + do_action( 'wp_memcached_get', $key, $group, $force, $found, $server_key, $by_key, $cache_cb, $cas_token ); $derived_key = $this->buildKey( $key, $group ); // Assume object is not found. @@ -1478,8 +1512,10 @@ public function get( $key, $group = 'default', $force = false, &$found = null, $ // If either $cache_db, or $cas_token is set, must hit Memcached and bypass runtime cache. if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups, true ) ) { if ( $by_key ) { + do_action( 'wp_memcached_external_get_by_key', $server_key, $derived_key, $cache_cb, $cas_token ); $value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token ); } else { + do_action( 'wp_memcached_external_get', $derived_key, $cache_cb, $cas_token ); $value = $this->m->get( $derived_key, $cache_cb, $cas_token ); } } else { @@ -1490,8 +1526,10 @@ public function get( $key, $group = 'default', $force = false, &$found = null, $ return false; } else { if ( $by_key ) { + do_action( 'wp_memcached_external_get_by_key', $server_key, $derived_key ); $value = $this->m->getByKey( $server_key, $derived_key ); } else { + do_action( 'wp_memcached_external_get', $derived_key ); $value = $this->m->get( $derived_key ); } } @@ -1530,6 +1568,7 @@ public function get( $key, $group = 'default', $force = false, &$found = null, $ * @return bool|mixed Cached object value. */ public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) { + do_action( 'wp_memcached_get_by_key', $server_key, $key, $group, $force, $found, $cache_cb, $cas_token ); /** * Need to be careful how "get" is called. If you send $cache_cb, and $cas_token, it will hit memcached. * Only send those args if they were sent to this function. @@ -1554,7 +1593,9 @@ public function getByKey( $server_key, $key, $group = 'default', $force = false, * @return bool True on success, false on failure. */ public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = null ) { + do_action( 'wp_memcached_get_delayed', $keys, $groups, $with_cas, $value_cb ); $derived_keys = $this->buildKeys( $keys, $groups ); + do_action( 'wp_memcached_external_get_delayed', $derived_keys, $with_cas, $value_cb ); return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb ); } @@ -1572,7 +1613,9 @@ public function getDelayed( $keys, $groups = 'default', $with_cas = false, $valu * @return bool True on success, false on failure. */ public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = null ) { + do_action( 'wp_memcached_get_delayed_by_key', $server_key, $keys, $groups, $with_cas, $value_cb ); $derived_keys = $this->buildKeys( $keys, $groups ); + do_action( 'wp_memcached_external_get_delayed_by_key', $server_key, $derived_keys, $with_cas, $value_cb ); return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb ); } @@ -1592,6 +1635,7 @@ public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_ * @return bool|array The array of found items on success, false on failure. */ public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = null, $flags = null ) { + do_action( 'wp_memcached_get_multi', $keys, $groups, $server_key, $cas_tokens, $flags ); $derived_keys = $this->buildKeys( $keys, $groups ); /** @@ -1601,8 +1645,10 @@ public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_to */ if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) { if ( ! empty( $server_key ) ) { + do_action( 'wp_memcached_external_get_multi_by_key', $server_key, $derived_keys, $cas_tokens, $flags ); $values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags ); } else { + do_action( 'wp_memcached_external_get_multi', $derived_keys, $cas_tokens, $flags ); $values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags ); } } else { @@ -1621,8 +1667,10 @@ public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_to // Get those keys not found in the runtime cache. if ( ! empty( $need_to_get ) ) { if ( ! empty( $server_key ) ) { + do_action( 'wp_memcached_external_get_multi_by_key', $server_key, array_keys( $need_to_get ) ); $result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) ); } else { + do_action( 'wp_memcached_external_get_multi', array_keys( $need_to_get ) ); $result = $this->m->getMulti( array_keys( $need_to_get ) ); } } @@ -1669,6 +1717,7 @@ public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_to * @return bool|array The array of found items on success, false on failure. */ public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = null, $flags = null ) { + do_action( 'wp_memcached_get_multi_by_key', $server_key, $keys, $groups, $cas_tokens, $flags ); /** * Need to be careful how "getMulti" is called. If you send $cache_cb, and $cas_token, it will hit memcached. * Only send those args if they were sent to this function. @@ -1691,6 +1740,7 @@ public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_to * the cache contents on success, or false on failure. */ public function getMultiple( $keys, $group = '', $force = false ) { + do_action( 'wp_memcached_get_multiple', $keys, $group, $force ); $values = array(); foreach ( $keys as $key ) { @@ -1711,6 +1761,8 @@ public function getMultiple( $keys, $group = '', $force = false ) { * @return mixed The value of the requested option on success, false on failure. */ public function getOption( $option ) { + do_action( 'wp_memcached_get_option', $option ); + do_action( 'wp_memcached_external_get_option', $option ); return $this->m->getOption( $option ); } @@ -1722,6 +1774,8 @@ public function getOption( $option ) { * @return int Result code of the last Memcached operation. */ public function getResultCode() { + do_action( 'wp_memcached_get_result_code' ); + do_action( 'wp_memcached_external_get_result_code' ); return $this->m->getResultCode(); } @@ -1733,6 +1787,8 @@ public function getResultCode() { * @return string Message describing the result of the last Memcached operation. */ public function getResultMessage() { + do_action( 'wp_memcached_get_result_message' ); + do_action( 'wp_memcached_external_get_result_message' ); return $this->m->getResultMessage(); } @@ -1745,6 +1801,8 @@ public function getResultMessage() { * @return array Array with host, post, and weight on success, false on failure. */ public function getServerByKey( $server_key ) { + do_action( 'wp_memcached_get_server_by_key', $server_key ); + do_action( 'wp_memcached_external_get_server_by_key', $server_key ); return $this->m->getServerByKey( $server_key ); } @@ -1756,6 +1814,8 @@ public function getServerByKey( $server_key ) { * @return array The list of all servers in the server pool. */ public function getServerList() { + do_action( 'wp_memcached_get_server_list' ); + do_action( 'wp_memcached_external_get_server_list' ); return $this->m->getServerList(); } @@ -1767,6 +1827,8 @@ public function getServerList() { * @return array Array of server statistics, one entry per server. */ public function getStats() { + do_action( 'wp_memcached_get_stats' ); + do_action( 'wp_memcached_external_get_stats' ); return $this->m->getStats(); } @@ -1778,6 +1840,8 @@ public function getStats() { * @return array Array of server versions, one entry per server. */ public function getVersion() { + do_action( 'wp_memcached_get_version' ); + do_action( 'wp_memcached_external_get_version' ); return $this->m->getVersion(); } @@ -1792,6 +1856,7 @@ public function getVersion() { * @return int|bool Item's new value on success, false on failure. */ public function increment( $key, $offset = 1, $group = 'default' ) { + do_action( 'wp_memcached_increment', $key, $offset, $group ); $derived_key = $this->buildKey( $key, $group ); // Increment values in no_mc_groups. @@ -1818,6 +1883,7 @@ public function increment( $key, $offset = 1, $group = 'default' ) { } } + do_action( 'wp_memcached_external_increment', $derived_key, $offset ); $result = $this->m->increment( $derived_key, $offset ); if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { @@ -1841,6 +1907,7 @@ public function increment( $key, $offset = 1, $group = 'default' ) { * @return int|bool Item's new value on success, false on failure. */ public function incr( $key, $offset = 1, $group = 'default' ) { + do_action( 'wp_memcached_incr', $key, $offset, $group ); return $this->increment( $key, $offset, $group ); } @@ -1865,6 +1932,7 @@ public function incr( $key, $offset = 1, $group = 'default' ) { * @return bool True on success, false on failure. */ public function prepend( $key, $value, $group = 'default', $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_prepend', $key, $value, $group, $server_key, $by_key ); if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) { return false; } @@ -1884,8 +1952,10 @@ public function prepend( $key, $value, $group = 'default', $server_key = '', $by // Append to Memcached value. if ( $by_key ) { + do_action( 'wp_memcached_external_prepend_by_key', $server_key, $derived_key, $value ); $result = $this->m->prependByKey( $server_key, $derived_key, $value ); } else { + do_action( 'wp_memcached_external_prepend', $derived_key, $value ); $result = $this->m->prepend( $derived_key, $value ); } @@ -1918,6 +1988,7 @@ public function prepend( $key, $value, $group = 'default', $server_key = '', $by * @return bool True on success, false on failure. */ public function prependByKey( $server_key, $key, $value, $group = 'default' ) { + do_action( 'wp_memcached_prepend_by_key', $server_key, $key, $value, $group ); return $this->prepend( $key, $value, $group, $server_key, true ); } @@ -1938,6 +2009,7 @@ public function prependByKey( $server_key, $key, $value, $group = 'default' ) { * @return bool True on success, false on failure. */ public function replace( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_replace', $key, $value, $group, $expiration, $server_key, $by_key ); $derived_key = $this->buildKey( $key, $group ); // If group is a non-Memcached group, save to runtime cache, not Memcached. @@ -1956,8 +2028,10 @@ public function replace( $key, $value, $group = 'default', $expiration = 0, $ser // Save to Memcached. if ( $by_key ) { + do_action( 'wp_memcached_external_replace_by_key', $server_key, $derived_key, $value, $expiration ); $result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration ); } else { + do_action( 'wp_memcached_external_replace', $derived_key, $value, $expiration ); $result = $this->m->replace( $derived_key, $value, $expiration ); } @@ -1985,6 +2059,7 @@ public function replace( $key, $value, $group = 'default', $expiration = 0, $ser * @return bool True on success, false on failure. */ public function replaceByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) { + do_action( 'wp_memcached_replace_by_key', $server_key, $key, $value, $group, $expiration ); return $this->replace( $key, $value, $group, $expiration, $server_key, true ); } @@ -2004,6 +2079,7 @@ public function replaceByKey( $server_key, $key, $value, $group = 'default', $ex * @return bool True on success, false on failure. */ public function set( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_set', $key, $value, $group, $expiration, $server_key, $by_key ); $derived_key = $this->buildKey( $key, $group ); // If group is a non-Memcached group, save to runtime cache, not Memcached. @@ -2016,8 +2092,10 @@ public function set( $key, $value, $group = 'default', $expiration = 0, $server_ // Save to Memcached. if ( $by_key ) { + do_action( 'wp_memcached_external_set_by_key', $server_key, $derived_key, $value, $expiration ); $result = $this->m->setByKey( $server_key, $derived_key, $value, $expiration ); } else { + do_action( 'wp_memcached_external_set', $derived_key, $value, $expiration ); $result = $this->m->set( $derived_key, $value, $expiration ); } @@ -2044,6 +2122,7 @@ public function set( $key, $value, $group = 'default', $expiration = 0, $server_ * @return bool True on success, false on failure. */ public function setByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) { + do_action( 'wp_memcached_set_by_key', $server_key, $key, $value, $group, $expiration ); return $this->set( $key, $value, $group, $expiration, $server_key, true ); } @@ -2066,6 +2145,7 @@ public function setByKey( $server_key, $key, $value, $group = 'default', $expira * @return bool True on success, false on failure. */ public function setMulti( $items, $groups = 'default', $expiration = 0, $server_key = '', $by_key = false ) { + do_action( 'wp_memcached_set_multi', $items, $groups, $expiration, $server_key, $by_key ); // Build final keys and replace $items keys with the new keys. $derived_keys = $this->buildKeys( array_keys( $items ), $groups ); $derived_items = array_combine( $derived_keys, $items ); @@ -2087,8 +2167,10 @@ public function setMulti( $items, $groups = 'default', $expiration = 0, $server_ // Save to memcached. if ( $by_key ) { + do_action( 'wp_memcached_external_set_multi_by_key', $server_key, $derived_items, $expiration ); $result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration ); } else { + do_action( 'wp_memcached_external_set_multi', $derived_items, $expiration ); $result = $this->m->setMulti( $derived_items, $expiration ); } @@ -2118,6 +2200,7 @@ public function setMulti( $items, $groups = 'default', $expiration = 0, $server_ * @return bool True on success, false on failure. */ public function setMultiByKey( $server_key, $items, $groups = 'default', $expiration = 0 ) { + do_action( 'wp_memcached_set_multi_by_key', $server_key, $items, $groups, $expiration ); return $this->setMulti( $items, $groups, $expiration, $server_key, true ); } @@ -2132,6 +2215,7 @@ public function setMultiByKey( $server_key, $items, $groups = 'default', $expira * true on success, or false on failure. */ public function setMultiple( array $items, $group = '', $expiration = 0 ) { + do_action( 'wp_memcached_set_multiple', $items, $group, $expiration ); $values = array(); foreach ( $items as $key => $value ) { @@ -2151,6 +2235,8 @@ public function setMultiple( array $items, $group = '', $expiration = 0 ) { * @return bool True on success, false on failure. */ public function setOption( $option, $value ) { + do_action( 'wp_memcached_set_option', $option, $value ); + do_action( 'wp_memcached_external_set_option', $option, $value ); return $this->m->setOption( $option, $value ); } @@ -2167,6 +2253,7 @@ public function setOption( $option, $value ) { * @return string */ public function buildKey( $key, $group = 'default' ) { + do_action( 'wp_memcached_build_key', $key, $group ); if ( empty( $group ) ) { $group = 'default'; } @@ -2197,6 +2284,7 @@ public function buildKey( $key, $group = 'default' ) { * @return array Array that combines keys and groups into a single set of memcached keys. */ public function buildKeys( $keys, $groups = 'default' ) { + do_action( 'wp_memcached_build_keys', $keys, $groups ); $derived_keys = array(); // If strings sent, convert to arrays for proper handling. @@ -2243,6 +2331,7 @@ public function buildKeys( $keys, $groups = 'default' ) { * @return string|int The sanitized expiration time. */ public function sanitize_expiration( $expiration ) { + do_action( 'wp_memcached_sanitize_expiration', $expiration ); if ( $expiration > $this->thirty_days && $expiration <= $this->now ) { $expiration = $expiration + $this->now; } @@ -2263,6 +2352,7 @@ public function sanitize_expiration( $expiration ) { * @return mixed Combined value casted to the type of the first value. */ public function combine_values( $original, $pended, $direction ) { + do_action( 'wp_memcached_combine_values', $original, $pended, $direction ); $type = gettype( $original ); // Combine the values based on direction of the "pend". @@ -2285,6 +2375,7 @@ public function combine_values( $original, $pended, $direction ) { * @param mixed $value Object value. */ public function add_to_internal_cache( $derived_key, $value ) { + do_action( 'wp_memcached_add_to_internal_cache', $derived_key, $value ); if ( is_object( $value ) ) { $value = clone $value; } @@ -2299,6 +2390,7 @@ public function add_to_internal_cache( $derived_key, $value ) { * @return bool True if a no_mc_group is present; false if a no_mc_group is not present. */ public function contains_no_mc_group( $groups ) { + do_action( 'wp_memcached_contains_no_mc_group', $groups ); if ( is_scalar( $groups ) ) { return in_array( $groups, $this->no_mc_groups, true ); } @@ -2327,6 +2419,7 @@ public function contains_no_mc_group( $groups ) { * @param array $groups Array of groups. */ public function add_global_groups( $groups ) { + do_action( 'wp_memcached_add_global_groups', $groups ); if ( ! is_array( $groups ) ) { $groups = (array) $groups; } @@ -2346,6 +2439,7 @@ public function add_global_groups( $groups ) { * @param array $groups Array of groups. */ public function add_non_persistent_groups( $groups ) { + do_action( 'wp_memcached_add_non_persistent_groups', $groups ); if ( ! is_array( $groups ) ) { $groups = (array) $groups; } @@ -2362,6 +2456,7 @@ public function add_non_persistent_groups( $groups ) { * @return bool|mixed Value on success, false on failure. */ public function get_from_runtime_cache( $key, $group ) { + do_action( 'wp_memcached_get_from_runtime_cache', $key, $group ); $derived_key = $this->buildKey( $key, $group ); if ( isset( $this->cache[ $derived_key ] ) ) { @@ -2377,6 +2472,7 @@ public function get_from_runtime_cache( $key, $group ) { * @param int $blog_id Blog to switch to. */ public function switch_to_blog( $blog_id ) { + do_action( 'wp_memcached_switch_to_blog', $blog_id ); global $table_prefix; $blog_id = (int) $blog_id; $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; diff --git a/tests/phpunit/tests/option/option.php b/tests/phpunit/tests/option/option.php index 36a40d9a2f495..4230899855354 100644 --- a/tests/phpunit/tests/option/option.php +++ b/tests/phpunit/tests/option/option.php @@ -548,4 +548,48 @@ public function test_add_option_clears_the_notoptions_cache() { $updated_notoptions = wp_cache_get( 'notoptions', 'options' ); $this->assertArrayNotHasKey( $option_name, $updated_notoptions, 'The "foobar" option should not be in the notoptions cache after adding it.' ); } + + /** + * Test that get_option() does not hit the external cache multiple times for the same option. + * + * @ticket 62692 + * + * @dataProvider data_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option + * + * @param bool $option_exists + * @param string $option_name + */ + public function test_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option( $option_exists = true, $option_name = 'ticket-62692' ) { + if ( $option_exists ) { + add_option( $option_name, 'value' ); + } + + $number_external_cache_hits = 0; + + add_action( + 'wp_memcached_external_get', + function () use ( &$number_external_cache_hits ) { + $number_external_cache_hits++; + } + ); + + $call_getter = 10; + while ( $call_getter-- ) { + get_option( $option_name ); + } + + $this->assertSame( 0, $number_external_cache_hits ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option() { + return array( + array( true ), + array( false ), + ); + } }