Skip to content

Instantly share code, notes, and snippets.

@daif
Created January 4, 2017 16:50
Show Gist options
  • Save daif/e18d9f0c92047e1415df57e8680e18c5 to your computer and use it in GitHub Desktop.
Save daif/e18d9f0c92047e1415df57e8680e18c5 to your computer and use it in GitHub Desktop.

Revisions

  1. daif created this gist Jan 4, 2017.
    16 changes: 16 additions & 0 deletions file_cache_contents.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <?php
    //Tiny Cache system
    function file_cache_contents($url, $cache_time=360000) { // 60*60*5
    $cache_dir = __DIR__.'/cache/';
    $cache_file = $cache_dir.md5($url).'.cache';
    @mkdir($cache_dir, 0775, true);
    // return data from cached file if file exists and not expired
    if(file_exists($cache_file) && filemtime($cache_file)+$cache_time >= time()) {
    return(unserialize(file_get_contents($cache_file)));
    }
    // get data from url and cache it then return the data
    $cache_data = file_get_contents($url);
    file_put_contents($cache_file, serialize($cache_data), LOCK_EX);
    return($cache_data);
    }
    ?>