WordPress.org

Making WordPress.org

Changeset 5138


Ignore:
Timestamp:
03/09/2017 09:18:39 PM (3 years ago)
Author:
iandunn
Message:

Events API: Cache the results of get_location()

Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 edited

Legend:

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

    r5137 r5138  
    1010
    1111    bootstrap();
     12    wp_cache_init();
    1213
    1314    // The test suite just needs the functions defined and doesn't want any headers or output
     
    1516        return;
    1617    }
    17 
    18     wp_cache_init();
    1918
    2019    $cache_group   = 'events';
     
    211210
    212211function get_location( $args = array() ) {
     212    global $cache_life, $cache_group;
     213
     214    $cache_key = 'get_location:' . md5( serialize( $args ) );
     215    $location  = wp_cache_get( $cache_key, $cache_group );
     216
     217    if ( false !== $location ) {
     218        return $location;
     219    }
    213220
    214221    // For a country request, no lat/long are returned.
     
    285292    }
    286293
     294    wp_cache_set( $cache_key, $location, $cache_group, $cache_life );
    287295    return $location;
    288296}
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5137 r5138  
    4949
    5050/**
     51 * Add a cachebusting parameter to bypass the object cache
     52 *
     53 * Cache keys are generated based on the function's input arguments (e.g., get_location()), so adding a unique
     54 * parameter on every function call ensures that the unit tests will never get a cached result.
     55 *
     56 * @param array $arguments
     57 *
     58 * @return array
     59 */
     60function add_cachebusting_parameter( $arguments ) {
     61    $arguments['cachebuster'] = microtime( true );
     62
     63    return $arguments;
     64}
     65
     66/**
    5167 * Test `get_location()`
    5268 *
     
    6076
    6177    foreach ( $cases as $case_id => $case ) {
     78        $case['input'] = add_cachebusting_parameter( $case['input'] );
    6279        $actual_result = get_location( $case['input'] );
    6380
     
    605622
    606623    foreach ( $cases as $case_id => $case ) {
     624        $case['input'] = add_cachebusting_parameter( $case['input'] );
    607625        $actual_result = get_city_from_coordinates( $case['input']['latitude'], $case['input']['longitude'] );
    608626        $passed        = $case['expected'] === $actual_result;
Note: See TracChangeset for help on using the changeset viewer.