note 110004 deleted from function.filemtime by aharvey
| From: | aharvey@php.net | Date: | Tue, 11 Sep 2012 01:51:02 +0000 |
| Subject: | note 110004 deleted from function.filemtime by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190617@lists.php.net to get a copy of this message | ||
Note Submitter: bzplan at web dot de
----
quick $ dirty ... update_cache (file) function
<?php
// ------------------------------------------------------------------
### header("Content-Type: text/html; charset=utf-8");
// ------------------------------------------------------------------
function update_cache($source_url,$cache_file,$cache_time=0,$time_stamp=0) {
if (empty($cache_time)) { $cache_time = (3600); }
if (empty($time_stamp)) { $time_stamp = time(); }
if (!file_exists($cache_file)) {
copy($source_url,$cache_file);
$status = 'new';
}
else {
$fmtime = filemtime($cache_file);
$txdiff = ($time_stamp - $fmtime);
if ($txdiff >= $cache_time) {
copy($source_url,$cache_file);
$status = 'update';
}
else {
$status = 'cached';
}
}
return $status;
}
// ------------------------------------------------------------------
### example / usage:
// ------------------------------------------------------------------
// ------ set your parameters ------
$source_url = 'http://twitter.com/statuses/user_timeline/php_net.xml';
// ---- source URL here
$cache_path = 'cache/';
$cache_name = md5($sourceurl);
$cache_type = 'xml';
$cache_file = $cache_path.'_'.$cache_name.'.'.$cache_type;
// ------ use: $cache_time in seconds ------
// ... e.g. (60*10) = 10 minutes
// ... e.g. (3600*6) = 6 hours
// ... e.g. (3600*24*2) = 2 days
$cache_time = (3600*24);
$time_stamp = time();
// ------------------------------------------------------------------
// ------ call the function ------
$cache_status = update_cache($source_url, $cache_file, $cache_time, $time_stamp);
// ------------------------------------------------------------------
// ------ print the return ------
print "<br />\n";
print "<br />Cache Status: ".$cache_status."\n";
print "<br />\n";
// ------------------------------------------------------------------
// ------ now use the $cache_file to read from ------
$data = simplexml_load_file($cache_file, null, LIBXML_NOCDATA);
print "<pre>\n";
print_r ($data);
print "</pre>\n";
// ------------------------------------------------------------------
?>
... have fun !!! .. :=)