Skip to content

Instantly share code, notes, and snippets.

@koshiaaaaan
Created November 9, 2010 04:30
Show Gist options
  • Save koshiaaaaan/668705 to your computer and use it in GitHub Desktop.
Save koshiaaaaan/668705 to your computer and use it in GitHub Desktop.
feedのキャッシュ
<?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