Plugin Directory

Changeset 800437


Ignore:
Timestamp:
11/07/2013 02:39:07 PM (12 years ago)
Author:
tobyberesford
Message:

Added caching

Location:
leaderboarded/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • leaderboarded/trunk/leaderboarded.php

    r687962 r800437  
    4141add_shortcode('leaderboarded', 'leaderboarded_board');
    4242function leaderboarded_board($atts) {
     43    // Merge Shortcode Parameters with some default ones
    4344    extract(shortcode_atts(array(
    4445        'slug' => 'gurus',
    4546        'release' => 'latest',
    46         'theme' => ''
     47        'theme' => 'default',
     48        'cut' => 'cut',
    4749    ), $atts));
    4850
    49     $url = 'http://www.leaderboarded.com/display/' . $slug . '/' . $release . '/embed/cut/' .$theme;
     51    // Create the Cache Key
     52    $cacheKey = 'leaderboarded:' . $slug . ':' . $release;
    5053
    51     $theBody = wp_remote_retrieve_body(wp_remote_get($url));
     54    // Create the URL for the Leaderboard that we're going to get
     55    $url = 'http://www.leaderboarded.com/display/' . $slug . '/' . $release . '/embed/' . $cut . '/' . $theme;
    5256
    53     return ($theBody) ? $theBody : 'Leaderboarded Plugin: could not find the leaderboard you asked for.';
     57    // Try getting the Cached Version (daily one)
     58    $theBody = wp_cache_get($cacheKey . ':' . 'd', 'leaderboarded');
     59
     60    // Try getting the Cached Version (weekly one)
     61    if ($theBody === false) {
     62        $theBody = wp_cache_get($cacheKey . ':' . 'w', 'leaderboarded');
     63    }
     64
     65    // Check we got something, if we didn't then try fetching a live one
     66    if ($theBody === false) {
     67        // Try fetching the Leaderboard from Leaderboarded.com
     68        $theBody = wp_remote_get($url);
     69        // Show a message when we get an error
     70        if (is_wp_error($theBody)) {
     71            return 'Leaderboarded Plugin: could not find the leaderboard you asked for.';
     72        } else {
     73            // Grab the Body and Cache the Leaderboard
     74            $theBody = wp_remote_retrieve_body($theBody);
     75
     76            // Cache both a daily and weekly copy
     77            wp_cache_set($cacheKey . ':' . 'd', $theBody, 'leaderboarded', 3600);
     78            wp_cache_set($cacheKey . ':' . 'w', $theBody, 'leaderboarded', (86400 * 7));
     79        }
     80    }
     81
     82    // Return the Leaderboard
     83    return $theBody;
    5484}
    5585
  • leaderboarded/trunk/readme.txt

    r685747 r800437  
    5656= 0.12 =
    5757* Updated to new URI schema
     58
     59= 0.13 =
     60* Added caching
Note: See TracChangeset for help on using the changeset viewer.