add( $key, $data, $group, $expire ); } function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->add_multiple( $data, $group, $expire ); } function wp_cache_incr( $key, $n = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->incr( $key, $n, $group ); } function wp_cache_decr( $key, $n = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->decr( $key, $n, $group ); } function wp_cache_close() { global $wp_object_cache; return $wp_object_cache->close(); } function wp_cache_delete( $key, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete( $key, $group ); } function wp_cache_delete_multiple( array $keys, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete_multiple( $keys, $group ); } function wp_cache_flush() { global $wp_object_cache; return $wp_object_cache->flush(); } function wp_cache_flush_runtime() { global $wp_object_cache; return $wp_object_cache->flush_runtime(); } function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { global $wp_object_cache; if ( function_exists( 'apply_filters' ) ) { $value = apply_filters( 'pre_wp_cache_get', false, $key, $group, $force ); if ( false !== $value ) { $found = true; return $value; } } return $wp_object_cache->get( $key, $group, $force, $found ); } function wp_cache_get_multiple( $keys, $group = '', $force = false ) { global $wp_object_cache; return $wp_object_cache->get_multiple( $keys, $group, $force ); } /** * Retrieve multiple cache entries * * @param array $groups Array of arrays, of groups and keys to retrieve * @return mixed */ function wp_cache_get_multi( $groups ) { global $wp_object_cache; return $wp_object_cache->get_multi( $groups ); } function wp_cache_init() { global $wp_object_cache; $wp_object_cache = new WP_Object_Cache(); } function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->replace( $key, $data, $group, $expire ); } function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; if ( defined( 'WP_INSTALLING' ) === false ) { return $wp_object_cache->set( $key, $data, $group, $expire ); } else { return $wp_object_cache->delete( $key, $group ); } } function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set_multiple( $data, $group, $expire ); } function wp_cache_switch_to_blog( $blog_id ) { global $wp_object_cache; return $wp_object_cache->switch_to_blog( $blog_id ); } function wp_cache_add_global_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_global_groups( $groups ); } function wp_cache_add_non_persistent_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_non_persistent_groups( $groups ); } /** * Determines whether the object cache implementation supports a particular feature. * * @param string $feature Name of the feature to check for. Possible values include: * 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple', * 'flush_runtime', 'flush_group'. * @return bool True if the feature is supported, false otherwise. */ function wp_cache_supports( $feature ) { switch ( $feature ) { case 'get_multiple': case 'flush_runtime': return true; default: return false; } } class WP_Object_Cache { var $global_groups = array( 'WP_Object_Cache_global' ); var $no_mc_groups = array(); var $cache = array(); /** @var Memcache[] */ var $mc = array(); var $default_mcs = array(); var $stats = array( 'get' => 0, 'get_local' => 0, 'get_multi' => 0, 'set' => 0, 'set_local' => 0, 'add' => 0, 'delete' => 0, 'delete_local' => 0, 'slow-ops' => 0, ); var $group_ops = array(); var $cache_hits = 0; var $cache_misses = 0; var $global_prefix = ''; var $blog_prefix = ''; var $key_salt = ''; var $flush_group = 'WP_Object_Cache'; var $global_flush_group = 'WP_Object_Cache_global'; var $flush_key = "flush_number_v4"; var $old_flush_key = "flush_number"; var $flush_number = array(); var $global_flush_number = null; var $cache_enabled = true; var $default_expiration = 0; var $max_expiration = 2592000; // 30 days var $stats_callback = null; var $connection_errors = array(); var $time_start = 0; var $time_total = 0; var $size_total = 0; var $slow_op_microseconds = 0.005; // 5 ms function add( $id, $data, $group = 'default', $expire = 0 ) { $key = $this->key( $id, $group ); if ( is_object( $data ) ) { $data = clone $data; } if ( in_array( $group, $this->no_mc_groups ) ) { if ( ! isset( $this->cache[ $key ] ) ) { $this->cache[ $key ] = [ 'value' => $data, 'found' => false, ]; return true; } return false; } if ( isset( $this->cache[ $key ][ 'value' ] ) && false !== $this->cache[ $key ][ 'value' ] ) { return false; } $mc = $this->get_mc( $group ); $expire = intval( $expire ); if ( 0 === $expire || $expire > $this->max_expiration ) { $expire = $this->default_expiration; } $size = $this->get_data_size( $data ); $this->timer_start(); $result = $mc->add( $key, $data, false, $expire ); $elapsed = $this->timer_stop(); $comment = ''; if ( isset( $this->cache[ $key ] ) ) { $comment .= ' [lc already]'; } if ( false === $result ) { $comment .= ' [mc already]'; } $this->group_ops_stats( 'add', $key, $group, $size, $elapsed, $comment ); if ( false !== $result ) { $this->cache[ $key ] = [ 'value' => $data, 'found' => true, ]; } else if ( false === $result && true === isset( $this->cache[$key][ 'value' ] ) && false === $this->cache[$key][ 'value' ] ) { /* * Here we unset local cache if remote add failed and local cache value is equal to `false` in order * to update the local cache anytime we get a new information from remote server. This way, the next * cache get will go to remote server and will fetch recent data. */ unset( $this->cache[$key] ); } return $result; } public function add_multiple( array $data, $group = '', $expire = 0 ) { $values = array(); foreach ( $data as $key => $value ) { $values[ $key ] = $this->add( $key, $value, $group, $expire ); } return $values; } function add_global_groups( $groups ) { if ( ! is_array( $groups ) ) { $groups = (array) $groups; } $this->global_groups = array_merge( $this->global_groups, $groups ); $this->global_groups = array_unique( $this->global_groups ); } function add_non_persistent_groups( $groups ) { if ( ! is_array( $groups ) ) { $groups = (array) $groups; } $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups ); $this->no_mc_groups = array_unique( $this->no_mc_groups ); } function incr( $id, $n = 1, $group = 'default' ) { $key = $this->key( $id, $group ); $mc = $this->get_mc( $group ); $incremented = $mc->increment( $key, $n ); $this->cache[ $key ] = [ 'value' => $incremented, 'found' => false !== $incremented, ]; return $this->cache[ $key ][ 'value' ]; } function decr( $id, $n = 1, $group = 'default' ) { $key = $this->key( $id, $group ); $mc = $this->get_mc( $group ); $decremented = $mc->decrement( $key, $n ); $this->cache[ $key ] = [ 'value' => $decremented, 'found' => false !== $decremented, ]; return $this->cache[ $key ][ 'value' ]; } function close() { foreach ( $this->mc as $bucket => $mc ) { $mc->close(); } } function delete( $id, $group = 'default' ) { $key = $this->key( $id, $group ); if ( in_array( $group, $this->no_mc_groups ) ) { unset( $this->cache[ $key ] ); return true; } $mc = $this->get_mc( $group ); $this->timer_start(); $result = $mc->delete( $key ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'delete', $key, $group, null, $elapsed ); if ( false !== $result ) { unset( $this->cache[ $key ] ); } return $result; } public function delete_multiple( array $keys, $group = '' ) { $values = array(); foreach ( $keys as $key ) { $values[ $key ] = $this->delete( $key, $group ); } return $values; } // Gets number from all default servers, replicating if needed function get_max_flush_number( $group ) { $key = $this->key( $this->flush_key, $group ); $values = array(); $size = 19; // size of microsecond timestamp serialized foreach ( $this->default_mcs as $i => $mc ) { $flags = false; $this->timer_start(); $values[ $i ] = $mc->get( $key, $flags ); $elapsed = $this->timer_stop(); if ( empty( $values[ $i ] ) ) { $this->group_ops_stats( 'get_flush_number', $key, $group, null, $elapsed, 'not_in_memcache' ); } else { $this->group_ops_stats( 'get_flush_number', $key, $group, $size, $elapsed, 'memcache' ); } } $max = max( $values ); if ( ! $max > 0 ) { return false; } // Replicate to servers not having the max. $expire = 0; foreach ( $this->default_mcs as $i => $mc ) { if ( $values[ $i ] < $max ) { $this->timer_start(); $mc->set( $key, $max, false, $expire ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'set_flush_number', $key, $group, $size, $elapsed, 'replication_repair' ); } } return $max; } function set_flush_number( $value, $group ) { $key = $this->key( $this->flush_key, $group ); $expire = 0; $size = 19; foreach ( $this->default_mcs as $i => $mc ) { $this->timer_start(); $mc->set( $key, $value, false, $expire ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'set_flush_number', $key, $group, $size, $elapsed, 'replication' ); } } function get_flush_number( $group ) { $flush_number = $this->get_max_flush_number( $group ); // What if there is no v4 flush number? if ( empty( $flush_number ) ) { // Look for the v3 flush number key. $flush_number = intval( $this->get( $this->old_flush_key, $group ) ); if ( !empty( $flush_number ) ) { // Found v3 flush number. Upgrade to v4 with replication. $this->set_flush_number( $flush_number, $group ); // Delete v3 key so we can't later restore it and find stale keys. } else { // No flush number found anywhere. Make a new one. This flushes the cache. $flush_number = $this->new_flush_number(); $this->set_flush_number( $flush_number, $group ); } } return $flush_number; } function get_global_flush_number() { if ( ! isset( $this->global_flush_number ) ) { $this->global_flush_number = $this->get_flush_number( $this->global_flush_group ); } return $this->global_flush_number; } function get_blog_flush_number() { if ( ! isset( $this->flush_number[ $this->blog_prefix ] ) ) { $this->flush_number[ $this->blog_prefix ] = $this->get_flush_number( $this->flush_group ); } return $this->flush_number[ $this->blog_prefix ]; } function flush_runtime() { $this->cache = array(); $this->group_ops = array(); return true; } function flush() { // Do not use the memcached flush method. It acts on an // entire memcached server, affecting all sites. // Flush is also unusable in some setups, e.g. twemproxy. // Instead, rotate the key prefix for the current site. // Global keys are rotated when flushing on any network's // main site. $this->cache = array(); $flush_number = $this->new_flush_number(); $this->rotate_site_keys( $flush_number ); if ( function_exists( 'is_main_site' ) && is_main_site() ) { $this->rotate_global_keys( $flush_number ); } return true; } function rotate_site_keys( $flush_number = null ) { if ( is_null( $flush_number ) ) { $flush_number = $this->new_flush_number(); } $this->set_flush_number( $flush_number, $this->flush_group ); $this->flush_number[ $this->blog_prefix ] = $flush_number; } function rotate_global_keys( $flush_number = null ) { if ( is_null( $flush_number ) ) { $flush_number = $this->new_flush_number(); } $this->set_flush_number( $flush_number, $this->global_flush_group ); $this->global_flush_number = $flush_number; } function new_flush_number() { return intval( microtime( true ) * 1e6 ); } function get( $id, $group = 'default', $force = false, &$found = null ) { $key = $this->key( $id, $group ); $mc = $this->get_mc( $group ); $found = true; if ( isset( $this->cache[ $key ] ) && ( ! $force || in_array( $group, $this->no_mc_groups ) ) ) { if ( isset( $this->cache[ $key ][ 'value' ] ) && is_object( $this->cache[ $key ][ 'value' ] ) ) { $value = clone $this->cache[ $key ][ 'value' ]; } else { $value = $this->cache[ $key ][ 'value' ]; } $found = $this->cache[ $key ][ 'found' ]; $this->group_ops_stats( 'get_local', $key, $group, null, null, 'local' ); } else if ( in_array( $group, $this->no_mc_groups ) ) { $this->cache[ $key ] = [ 'value' => $value = false, 'found' => false, ]; $found = false; $this->group_ops_stats( 'get_local', $key, $group, null, null, 'not_in_local' ); } else { $flags = false; $this->timer_start(); $value = $mc->get( $key, $flags ); $elapsed = $this->timer_stop(); // Value will be unchanged if the key doesn't exist. if ( false === $flags ) { $found = false; $value = false; } elseif ( false === $value && ( $flags & 0xFF01 ) === 0x01 ) { /* * The lowest byte is used for flags. * 0x01 means the value is serialized (MMC_SERIALIZED). * The second lowest indicates data type: 0 is string, 1 is bool, 3 is long, 7 is double. * `null` is serialized into a string, thus $flags is 0x0001 * Empty string will correspond to $flags = 0x0000 (not serialized). * For `false` or `true` $flags will be 0x0100 * * See: * - https://github.com/websupport-sk/pecl-memcache/blob/2a5de3c5d9c0bd0acbcf7e6e0b7570f15f89f55b/php7/memcache_pool.h#L61-L76 - PHP 7.x * - https://github.com/websupport-sk/pecl-memcache/blob/ccf702b14b18fce18a1863e115a7b4c964df952e/src/memcache_pool.h#L57-L76 - PHP 8.x * * In PHP 8, they changed the way memcache_get() handles `null`: * https://github.com/websupport-sk/pecl-memcache/blob/ccf702b14b18fce18a1863e115a7b4c964df952e/src/memcache.c#L2175-L2177 * * If the return value is `null`, it is silently converted to `false`. We can only rely upon $flags to find out whether `false` is real. */ $value = null; } $this->cache[ $key ] = [ 'value' => $value, 'found' => $found, ]; if ( is_null( $value ) || $value === false ) { $this->group_ops_stats( 'get', $key, $group, null, $elapsed, 'not_in_memcache' ); } else if ( 'checkthedatabaseplease' === $value ) { $this->group_ops_stats( 'get', $key, $group, null, $elapsed, 'checkthedatabaseplease' ); } else { $size = $this->get_data_size( $value ); $this->group_ops_stats( 'get', $key, $group, $size, $elapsed, 'memcache' ); } } if ( 'checkthedatabaseplease' === $value ) { unset( $this->cache[ $key ] ); $found = false; $value = false; } return $value; } function get_multi( $groups ) { /* format: $get['group-name'] = array( 'key1', 'key2' ); */ $return = array(); $return_cache = array( 'value' => false, 'found' => false, ); foreach ( $groups as $group => $ids ) { $mc = $this->get_mc( $group ); $keys = array(); $this->timer_start(); foreach ( $ids as $id ) { $key = $this->key( $id, $group ); $keys[] = $key; if ( isset( $this->cache[ $key ] ) ) { if ( is_object( $this->cache[ $key ][ 'value'] ) ) { $return[ $key ] = clone $this->cache[ $key ][ 'value']; $return_cache[ $key ] = [ 'value' => clone $this->cache[ $key ][ 'value'], 'found' => $this->cache[ $key ][ 'found'], ]; } else { $return[ $key ] = $this->cache[ $key ][ 'value']; $return_cache[ $key ] = [ 'value' => $this->cache[ $key ][ 'value' ], 'found' => $this->cache[ $key ][ 'found' ], ]; } continue; } else if ( in_array( $group, $this->no_mc_groups ) ) { $return[ $key ] = false; $return_cache[ $key ] = [ 'value' => false, 'found' => false, ]; continue; } else { $fresh_get = $mc->get( $key ); $return[ $key ] = $fresh_get; $return_cache[ $key ] = [ 'value' => $fresh_get, 'found' => false !== $fresh_get, ]; } } $elapsed = $this->timer_stop(); $this->group_ops_stats( 'get_multi', $keys, $group, null, $elapsed ); } $this->cache = array_merge( $this->cache, $return_cache ); return $return; } public function get_multiple( $keys, $group = 'default', $force = false ) { $mc = $this->get_mc( $group ); $no_mc = in_array( $group, $this->no_mc_groups, true ); $uncached_keys = array(); $return = array(); $return_cache = array(); foreach ( $keys as $id ) { $key = $this->key( $id, $group ); if ( isset( $this->cache[ $key ] ) && ( ! $force || $no_mc ) ) { $value = $this->cache[ $key ]['found'] ? $this->cache[ $key ]['value'] : false; $return[ $id ] = is_object( $value ) ? clone $value : $value; } else if ( $no_mc ) { $return[ $id ] = false; $return_cache[ $key ] = [ 'value' => false, 'found' => false, ]; } else { $uncached_keys[ $id ] = $key; } } if ( $uncached_keys ) { $this->timer_start(); $uncached_keys_list = array_values( $uncached_keys ); $values = $mc->get( $uncached_keys_list ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'get_multiple', $uncached_keys_list, $group, null, $elapsed ); foreach ( $uncached_keys as $id => $key ) { $found = array_key_exists( $key, $values ); $value = $found ? $values[ $key ] : false; $return[ $id ] = $value; $return_cache[ $key ] = [ 'value' => $value, 'found' => $found, ]; } } $this->cache = array_merge( $this->cache, $return_cache ); return $return; } function flush_prefix( $group ) { if ( $group === $this->flush_group || $group === $this->global_flush_group ) { // Never flush the flush numbers. $number = '_'; } elseif ( false !== array_search( $group, $this->global_groups ) ) { $number = $this->get_global_flush_number(); } else { $number = $this->get_blog_flush_number(); } return $number . ':'; } function key( $key, $group ) { if ( empty( $group ) ) { $group = 'default'; } $prefix = $this->key_salt; $prefix .= $this->flush_prefix( $group ); if ( false !== array_search( $group, $this->global_groups ) ) { $prefix .= $this->global_prefix; } else { $prefix .= $this->blog_prefix; } return preg_replace( '/\s+/', '', "$prefix:$group:$key" ); } function replace( $id, $data, $group = 'default', $expire = 0 ) { $key = $this->key( $id, $group ); $expire = intval( $expire ); if ( 0 === $expire || $expire > $this->max_expiration ) { $expire = $this->default_expiration; } $mc = $this->get_mc( $group ); if ( is_object( $data ) ) { $data = clone $data; } $size = $this->get_data_size( $data ); $this->timer_start(); $result = $mc->replace( $key, $data, false, $expire ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'replace', $key, $group, $size, $elapsed ); if ( false !== $result ) { $this->cache[ $key ] = [ 'value' => $data, 'found' => true, ]; } return $result; } function set( $id, $data, $group = 'default', $expire = 0 ) { $key = $this->key( $id, $group ); if ( isset( $this->cache[ $key ] ) && ( 'checkthedatabaseplease' === $this->cache[ $key ][ 'value' ] ) ) { return false; } if ( is_object( $data ) ) { $data = clone $data; } $this->cache[ $key ] = [ 'value' => $data, 'found' => false, // Set to false as not technically found in memcache at this point. ]; if ( in_array( $group, $this->no_mc_groups ) ) { $this->group_ops_stats( 'set_local', $key, $group, null, null ); return true; } $expire = intval( $expire ); if ( 0 === $expire || $expire > $this->max_expiration ) { $expire = $this->default_expiration; } $mc = $this->get_mc( $group ); $size = $this->get_data_size( $data ); $this->timer_start(); $result = $mc->set( $key, $data, false, $expire ); $elapsed = $this->timer_stop(); $this->group_ops_stats( 'set', $key, $group, $size, $elapsed ); // Update the found cache value with the result of the set in memcache. $this->cache[ $key ][ 'found' ] = $result; return $result; } public function set_multiple( array $data, $group = '', $expire = 0 ) { $values = array(); foreach ( $data as $key => $value ) { $values[ $key ] = $this->set( $key, $value, $group, $expire ); } return $values; } function switch_to_blog( $blog_id ) { global $table_prefix; $blog_id = (int) $blog_id; $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ); } function colorize_debug_line( $line, $trailing_html = '' ) { $colors = array( 'get' => 'green', 'get_local' => 'lightgreen', 'get_multi' => 'fuchsia', 'get_multiple' => 'navy', 'set' => 'purple', 'set_local' => 'orchid', 'add' => 'blue', 'delete' => 'red', 'delete_local' => 'tomato', 'slow-ops' => 'crimson', ); $cmd = substr( $line, 0, strpos( $line, ' ' ) ); // Start off with a neutral default color... $color_for_cmd = 'brown'; // And if the cmd has a specific color, use that instead if ( isset( $colors[ $cmd ] ) ) { $color_for_cmd = $colors[ $cmd ]; } $cmd2 = "" . esc_html( $cmd ) . ""; return $cmd2 . esc_html( substr( $line, strlen( $cmd ) ) ) . "$trailing_html\n"; } function js_toggle() { echo " "; } /** * Returns the collected raw stats. * * @return array $stats */ function get_stats() { $stats = []; $stats['totals'] = [ 'query_time' => $this->time_total, 'size' => $this->size_total, ]; $stats['operation_counts'] = $this->stats; $stats['operations'] = []; $stats['groups'] = []; $stats['slow-ops'] = []; $stats['slow-ops-groups'] = []; foreach ( $this->group_ops as $cache_group => $dataset ) { if ( empty( $cache_group ) ) { $cache_group = 'default'; } foreach ( $dataset as $data ) { $operation = $data[0]; $op = [ 'key' => $data[1], 'size' => $data[2], 'time' => $data[3], 'group' => $cache_group, 'result' => $data[4], ]; if ( $cache_group === 'slow-ops' ) { $key = 'slow-ops'; $groups_key = 'slow-ops-groups'; $op['group'] = $data[5]; $op['backtrace'] = $data[6]; } else { $key = 'operations'; $groups_key = 'groups'; } $stats[ $key ][ $operation ][] = $op; if ( ! in_array( $op['group'], $stats[ $groups_key ] ) ) { $stats[ $groups_key ][] = $op['group']; } } } return $stats; } function stats() { $this->js_toggle(); echo '