Making WordPress.org


Ignore:
Timestamp:
06/08/2017 03:46:45 AM (9 years ago)
Author:
iandunn
Message:

Events: Move caching deeper into logic to restore broad cache hits

Previously the caching was done at a higher level, for simplicity. At the time this was setup, Core would either pass the IP, _or_ the location search, but not both. So, the arguments passed to get_location() were unique enough to generate a cache key from.

That changed with ​[wp40774], and now Core always passes the IP, even if it's also passing a search query. Because of that, the arguments to get_location() are no longer distinct enough to use as cache keys. For example, a search for Seattle would generate a cache key that was tied to the IP address that was passed, which lowers the cache hits by several orders of magnitude.

With this commit, the caching is moved down a level, to the controllers for IP geolocation and geocoding. They are only passed the arguments that are needed for their respective queries, so the arguments are distinct enough to generate appropriate cache keys from.

Additionally, the value that gets cached when no location could be found was changed from false to __NOT_FOUND__, because wp_cache_set() / wp_cache_get() doesn't always appear to work when $data is boolean false.

Props dd32

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r5551 r5552  
    206206 */
    207207function guess_location_from_city( $location_name, $timezone, $country_code ) {
     208        global $cache_group, $cache_life;
     209
     210        $cache_key = 'guess_location_from_city:' . md5( $location_name . ':' . $timezone . ':' . $country_code );
     211        $guess     = wp_cache_get( $cache_key, $cache_group );
     212
     213        if ( $guess ) {
     214                if ( '__NOT_FOUND__' == $guess ) {
     215                        return false;
     216                }
     217
     218                return $guess;
     219        }
     220
    208221        $guess = guess_location_from_geonames( $location_name, $timezone, $country_code );
    209 
    210         if ( $guess ) {
    211                 return $guess;
    212         }
    213222
    214223        /*
    … …  
    231240                $guess     = guess_location_from_geonames( $city_name, $timezone, $country_code, $wildcard = false );
    232241        }
     242
     243        wp_cache_set( $cache_key, ( $guess ?: '__NOT_FOUND__' ), $cache_group, $cache_life );
    233244
    234245        return $guess;
    … …  
    304315 */
    305316function guess_location_from_ip( $dotted_ip ) {
    306         global $wpdb;
     317        global $wpdb, $cache_group, $cache_life;
     318
     319        $cache_key = 'guess_location_from_ip:' . $dotted_ip;
     320        $location  = wp_cache_get( $cache_key, $cache_group );
     321
     322        if ( $location !== false ) {
     323                if ( '__NOT_FOUND__' == $location ) {
     324                        return false;
     325                }
     326
     327                return $location;
     328        }
    307329
    308330        $long_ip = false;
    … …  
    319341
    320342        if ( false === $long_ip || ! isset( $from, $where ) ) {
     343                wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life );
    321344                return false;
    322345        }
    … …  
    333356        // Unknown location:
    334357        if ( ! $row || '-' == $row->country_short ) {
     358                wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life );
    335359                return false;
    336360        }
     361
     362        wp_cache_set( $cache_key, $row, $cache_group, $cache_life );
    337363
    338364        return $row;
    … …  
    444470        global $cache_life, $cache_group;
    445471
    446         $cache_key = 'get_location:' . md5( serialize( $args ) );
    447         $location  = wp_cache_get( $cache_key, $cache_group );
    448472        $throttle_geonames = $throttle_ip2location = false;
    449 
    450         if ( false !== $location ) {
    451                 return $location;
    452         }
    453473
    454474        // For a country request, no lat/long are returned.
    … …  
    458478                );
    459479        }
    460 
    461         $country_code = get_country_code_from_locale( $args['locale'] ?? '' );
    462480
    463481        // Coordinates provided
    … …  
    483501                }
    484502
     503                $country_code = get_country_code_from_locale( $args['locale'] ?? '' );
    485504                $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code  );
    486505
    … …  
    535554        }
    536555
    537         if ( $location !== 'temp-request-throttled' ) {
    538                 wp_cache_set( $cache_key, $location, $cache_group, $cache_life );
    539         }
    540 
    541556        return $location;
    542557}
    … …  
    579594 */
    580595function guess_location_from_country( $location_name ) {
     596        global $cache_group, $cache_life;
     597
     598        $cache_key = 'guess_location_from_country:' . $location_name;
     599        $country   = wp_cache_get( $cache_key, $cache_group );
     600
     601        if ( $country ) {
     602                if ( '__NOT_FOUND__' == $country ) {
     603                        return false;
     604                }
     605
     606                return $country;
     607        }
     608
    581609        // Check if they entered only the country name, e.g. "Germany" or "New Zealand"
    582610        $country             = get_country_from_name( $location_name );
    … …  
    613641                $country = get_country_from_name( $country_name );
    614642        }
     643
     644        wp_cache_set( $cache_key, ( $country ?: '__NOT_FOUND__' ), $cache_group, $cache_life );
    615645
    616646        return $country;
Note: See TracChangeset for help on using the changeset viewer.