Skip to content

Instantly share code, notes, and snippets.

View mibrahim's full-sized avatar

Mohamed Ibrahim mibrahim

View GitHub Profile
@mibrahim
mibrahim / cache.php
Created May 31, 2017 19:27
php caching
https://davidwalsh.name/php-cache-function
/* gets the contents of a file if it exists, otherwise grabs and caches */
function get_content($file,$url,$hours = 24,$fn = '',$fn_args = '') {
//vars
$current_time = time(); $expire_time = $hours * 60 * 60; $file_time = filemtime($file);
//decisions, decisions
if(file_exists($file) && ($current_time - $expire_time < $file_time)) {
//echo 'returning from cached file';
return file_get_contents($file);