• I was using this code to cache delicious feeds in my sidebar; however whenever it couldn’t access delicious it would replace my feed with an ugly warning message. The delicious cached plugin does this better – but it falls short in that it doesn’t allow for multiple feeds. I solve this problem by creating a top level folder (writable by the system) called “delicious” and putting multiple copies of my own script with slightly different names for each feed. i.e. “blogroll.inc” or “favorites.inc” – each producing a differently named cache file in the same folder. My script is an amalgam of the two scripts mentioned above, and I thought I’d share it because this has been very useful for me. Hopefully someone will eventually write a more robust delicious caching plugin which allows for multiple feeds from different tags.

    This example uses the tag “blogroll” and produces a cached file: “blogroll.html.”

    <?php
    $username = "USERNAME";
    $user = "USERNAME/blogroll";
    $opts = "?count=20&tags=no&rssbutton=no&bullet=";
    $url = "https://del.icio.us/html/";
    $cachefile = "delicious/blogroll.html"; // name of cache file
    $cachetime = 60 * 60; // 60 minutes

    // if cache is valid, just use it instead of hitting del.icio.us
    if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))
    {
    include($cachefile);
    } else {
    $c = curl_init($url . $user . $opts);
    // cURL options
    curl_setopt($c, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($c, CURLOPT_USERPWD,"$username");
    curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($c, CURLOPT_TIMEOUT, 4);
    curl_setopt($c, CURLOPT_USERAGENT, "del.icio.us cached 1.4 / https://www.w-a-s-a-b-i.com");
    $response = curl_exec($c);
    $info = curl_getinfo($c);
    $_curl_error_code = $info['http_code'];

    if($_curl_error_code == 200) {
    echo $response;

    $fp = fopen($cachefile, 'w');
    fwrite($fp, $response);
    fclose($fp);
    } else {
    include($cachefile);
    }
    }
    ?>

  • The topic ‘Improved Del.icio.us caching’ is closed to new replies.