Exemple #1
0
function getConversionRate($from, $to)
{
    global $cache_tolerance, $cache_conversion, $webServiceURL;
    $cache_file = $cache_conversion . md5($from) . "-" . md5($to);
    if (isCacheValid($cache_file, $cache_tolerance)) {
        return file_get_contents($cache_file);
    }
    $url = $webServiceURL . "?from=" . $from . "&to=" . $to;
    $cache_contents = updateCache($cache_file, $url);
    if (strlen($cache_contents) > 0) {
        $cache_file_reverse = $cache_conversion . md5($to) . "-" . md5($from);
        $reverse_response = getReverseResponse($cache_contents);
        if (strlen($reverse_response) > 0) {
            writeCache($cache_file_reverse, $reverse_response);
        }
    } else {
        $cache_contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response>\n<conversion-rate>0.0</conversion-rate>\n<date>:-(</date>\n</response>";
    }
    return $cache_contents;
}
    }
    //check if the file needs to be refreshed or not ?
    $last_modified = new DateTime('@' . filemtime($cache));
    $expire = new DateTime('-' . $ttl, new DateTimezone('UTC'));
    return $last_modified > $expire;
}
function truncateCache()
{
    global $cachefil;
    $fh = fopen($cachefil, 'w');
    fclose($fh);
}
// Cache-settings
// Recipe from: http://nyamsprod.com/blog/2014/tutorial-how-to-cache-a-resource-using-php/
$geojson = "";
if (!isCacheValid($cachefil, $ttl)) {
    header('Hurtigbuffer: bom');
    // if last cache hit was an error, invalidate immediately
    // so that no more requests to source is triggered by request by another user
    // happening before fetchData()-timeout.
    if (filesize($cachefil) == 0) {
        truncateCache();
    }
    $geojson = fetchData();
    header('Content-Type: application/vnd.geo+json; charset=utf-8');
    file_put_contents($cachefil, $geojson);
} else {
    header('Hurtigbuffer: treff');
    header('Content-Type: application/vnd.geo+json; charset=utf-8');
    header('Hurtigbuffer-size: ' . filesize($cachefil));
    if (filesize($cachefil) == 0) {