$buf
July 28, 2000
Here we determine whether $buf can instead be built from the
$cache_file by checking the following:
- if cache parameter is set and less than 10, we are forced to skip the
cache!
- if cache file exists and the cache parameter is not set, check if
default cache time has not yet expired, for then we can use the cache
and we should not update it
- if cache file exists and cache parameter is set, check if that time has
not yet expired, for then we can use the cache and we should not update
it
- if we should not use the cache because it is expired, we'll update it
after we executed the module
Thus we can now make the $buf creation conditional to the
$read_cache variable, by replacing the line that says:
<?php $buf = $fun ($arglist); ?>
with the the following code:
<?php
if ($read_cache || (!strlen ($buf .= $fun ($arglist)))) {
if ($f = fopen ($cache_file, "r")) {
while ($str = fgets ($f, 4096)) {
$buf .= $str;
}
fclose ($f);
} else $buf .= "<!-- $tag: error - cache is empty //-->\n";
}
?>
Note that the module can force the cache (e.g. when database is down) by
returning an empty string (risking the cache has not yet been written,
but in any case it does not generate a database error, but here an empty
place in the page with an html comment)
The last step is see if we should also update the cache, by continuing to
add the following code before return $buf:
<?php
if ($write_cache && ($f = fopen ($cache_file, "w"))) {
fputs ($f, $buf);
fclose ($f);
}
?>
Now the parser is done and you can start creating both the res/ and cache/
subdirectories and fill res/ with modules xxx.php which implements the
function handle_xxx ($arglist)
The parse_it function
Building your website with cached dynamic modules
Conclusion
|