Created
October 17, 2011 17:22
-
-
Save chrismeller/1293165 to your computer and use it in GitHub Desktop.
Fuel Cache Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private function add_external_assets ( ) { | |
$url = 'http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css'; | |
try { | |
$content = Cache::get( md5( $url ) ); | |
} | |
catch ( CacheNotFoundException $e ) { | |
$content = null; | |
} | |
if ( $content === null ) { | |
$content = $this->get_external_asset( $url ); | |
} | |
Casset::css_inline( $content ); | |
$url = 'http://twitter.github.com/bootstrap/1.3.0/bootstrap-dropdown.js'; | |
try { | |
$content = Cache::get( md5( $url ) ); | |
} | |
catch ( CacheNotFoundException $e ) { | |
$content = null; | |
} | |
if ( $content === null ) { | |
$content = $this->get_external_asset( $url ); | |
} | |
Casset::js_inline( $content ); | |
} | |
private function get_external_asset ( $url, $cache = true ) { | |
$options = array( | |
'http' => array( | |
'timeout' => 3, | |
), | |
); | |
$context = stream_context_create( $options ); | |
$contents = file_get_contents( $url, false, $context ); | |
// get the expires header | |
$headers = $this->parse_headers( $http_response_header ); | |
if ( !isset( $headers['Expires'] ) ) { | |
$expires = time() + ( 24 * 60 * 60 ); // 24 hours from now | |
} | |
else { | |
$expires = new DateTime( $headers['Expires'] ); | |
$expires = $expires->format( 'U' ); | |
} | |
// are we supposed to cache it? | |
if ( $cache ) { | |
Cache::set( md5( $url ), $contents, $expires ); | |
} | |
return $contents; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment