PHPBuilder - Cached Dynamic Modules Page 5



RSS Twitter
Articles Site Operation

Cached Dynamic Modules - Page 5

by: JP
|
July 30, 2000

Here we determine whether $buf can instead be built from the $cache_file by checking the following:
  • if cache paramer 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 ($f4096)) {
            
$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);
}

?>
v Now the parser is done and you can start creating both the res/ and cache/ subdirectories and fill res/ with modules xxx.php that each implements the function handle_xxx ($arglist).

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
JP

Comment:



Comment:

(Maximum characters: 1200). You have characters left.