LoginSignup
2
1

More than 5 years have passed since last update.

file_get_contentsをキャッシュ付きにする

Last updated at Posted at 2017-10-15

あるAPIを試していたら、あっという間に試用上限に達してしまったので、早い段階でキャッシュする習慣にしたほうがいいかな、と。そのうち多機能にします。

function file_get_contents_cache($url){
    $cache_path = "cache/".md5($url);
    if( file_exists($cache_path) ){
        $contents = file_get_contents($cache_path);
    }else{
        $contents = file_get_contents($url);
        file_put_contents($cache_path,$contents);
    }
    return $contents;
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1