Search the Community
Showing results for tags 'caching'.
-
Hi I just have a question about the caching code below. Should the date function be in the filename if I want to cache the file for several days? Or if the date changes will a new file be created and used instead of the the previous cache file. Is that not even used since my if statement checks for the file modification time? Thanks for the help in understanding this. $cachefile = 'cache/cached/'.$id . date('M-d-Y').'.php'; $cachetime = 172800; // Check if the cached file is still fresh. If it is, serve it up and exit. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { include($cachefile); exit; } ob_start(); {HTML TO CACHE} $fp = fopen($cachefile, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); // finally send browser output ob_end_flush(); I basically want to cache a file for 2-3 days.
-
I have been researching some strategies to optimize a web application I am working on particularly related to web browser caching and dynamic data. Since potentially the same dynamic content may be loaded multiple times in a session, I came up with the following method using PHP's output buffer and using a hash of the content as an ETag. I realize that the only thing I really save with this method is the transfer of data back to the user since the PHP script still has to completely run, but I was curious if anyone has done something similar and if there are any thoughts or concerns I should be aware of or what other methods may be better. Here is the code I am including at the top of each page: <?php function hash_buffer($content) { $buffer_hash = crc32($content); if ($_SERVER['HTTP_IF_NONE_MATCH'] == $buffer_hash) { header('HTTP/1.1 304 Not Modified'); header("ETag: $buffer_hash"); return ''; } header('Cache-Control: private, no-cache'); header("ETag: $buffer_hash"); return $content; } ob_start('hash_buffer'); ?>
- 3 replies
-
- caching
- output buffer
-
(and 2 more)
Tagged with: