Skip to content

Instantly share code, notes, and snippets.

@thisislawatts
Created September 2, 2013 14:38
Show Gist options
  • Save thisislawatts/6413556 to your computer and use it in GitHub Desktop.
Save thisislawatts/6413556 to your computer and use it in GitHub Desktop.
Speedy Cache
<?php
/**
* Speedy cache
*
*/
$url = "http://thisis.la";
$file_path = getcwd() . '/cache';
if ( is_expired( $file_path ) ) {
$output = get_url( $url );
$fh = fopen( $file_path , 'w');
fwrite( $fh, watermark( $output ) );
} else {
$output = file_get_contents( $file_path );
}
echo $output;
function is_expired( $file_path, $expiry_age = 1800 ) {
if ( !file_exists( $file_path ) ) return true;
return (mktime() - filemtime( $file_path )) >= $expiry_age;
}
function file_writable( $file_path ) {
return $fh = fopen( $file_path, 'w');
}
function get_url( $url ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Grab
if ( !$result = curl_exec($ch) ) {
trigger_error('Failed to fetch ' . $url );
}
curl_close($ch);
return $result;
}
function watermark( $output ) {
return $output .= '<!-- Cached at '. date("Y/m/d G:i:s") .' -->';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment