Create a gist now

Instantly share code, notes, and snippets.

@Nodws /Cache
Created Aug 24, 2017

What would you like to do?
<?php
$cache = 'data.txt';
if(date("Ymd", filemtime($cache)) < date("Ymd") || filesize($cache) < 1):
$data = get_data(); // do your thing
$file = fopen($cache,'w+');
$text = is_array($data) ? json_encode($data) : $data;
fwrite($file, $text);
fclose($file);
else:
$file = fopen($cache,'w+');
$data = fread($file, filesize($cache));
if($data[0] == '{');
$data = json_decode($data, true);
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment