Created
November 9, 2010 04:30
-
-
Save koshiaaaaan/668705 to your computer and use it in GitHub Desktop.
feedのキャッシュ
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 | |
$cache_dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'cache' ; | |
if( realpath( $cache_dir ) === false ) { | |
if( ! mkdir( $cache_dir, 0777 ) ) { | |
exit( 'Error!' ) ; | |
} | |
} | |
$cache_dir = realpath( $cache_dir ) ; | |
$now = time() ; | |
$lifetime = 3600 ; | |
$feeds[ 'kachibito' ] = 'http://kachibito.net/feed' ; | |
foreach( $feeds as $name => $feed ) { | |
$file = $cache_dir . DIRECTORY_SEPARATOR . md5( $feed ) ; | |
if( file_exists( $file ) && ( $now - $lifetime ) < filemtime( $file ) ) { | |
$output[ $name ] = file_get_contents( $file ) ; | |
continue ; | |
} | |
$output[ $name ] = file_get_contents( $feed ) ; | |
if( $output[ $name ] !== false ) { | |
file_put_contents( $file, $output[ $name ], LOCK_EX ) ; | |
touch( $file, $now ) ; | |
} else { | |
$output[ $name ] = 'フィード [ ' . $feed . ' ] は読み込めませんでした。' ; | |
} | |
} | |
echo htmlentities( $output[ 'kachibito' ], ENT_QUOTES, 'UTF-8' ) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment