Jump to content

[SOLVED] Cache Setting


robindean

Recommended Posts

I'm using one large php doc to store my entire line of content.

 

It's only 40k total, but I'd like to cache the entire file for faster serving regardless.

 

The way my site loads, it queries the ? string at the end of a url and prints based on the result.

 

I'd like to cache the ENTIRE file so that when people surf, it feels "instantanious".

 

Methods? Suggestions?

Link to comment
Share on other sites

Put the cache headers on the top of the file like this:-

 

header("Cache-Control: public");
header("Expires: " . gmdate('D, d M Y H:i:s', time()+(3600*24)) . " GMT");

 

The above code will cache the file for 24 hours on the user's machine. But the file will have to be downloaded at least once and only subsequent requests will get served from the cache

Link to comment
Share on other sites

Does this mean that the file would download once (upon first visit) and then all new query strings would be loaded from the cached file?

 

i.e. I'm using things like: pagetitle.php?display=1thing vs. pagetitle.php?display=anotherthing

 

would differences in the ? string force the viewer to download the entire 40k file again or would it load from cache?

Link to comment
Share on other sites

PHP is processed on the server side, so for the PHP to output something different, the server needs to process it again and upload it to the client again. Meaning that every single page needs to be downloaded at least once before it can be displayed.

 

And they don't have to download the entire php file, only the stuff that you send them with echo, print, die, etc..

Link to comment
Share on other sites

You can use output buffering to capture your php scripts output before it is sent to the browser, then store the output in a html file. Then change the links on your website to point to the html files instead of the php files. With some more coding, you can create one php script that will create cache files of your entire web directory. Of course this is all dependent on whether or not you host allows output buffering, and at what byte size.

 

Use the following code as a guide.

 

<?

ob_start();  //starts output buffering

include( $path_to_php_script ); // when a php script is included, it is first executed, so the include function returns the html output of the php file
$html = ob_get_contents(); // gets the contents of the output buffer, which is your html, and stores them in $html

ob_end_clean(); // ends output buffering, and does not output the buffer to browser

$new_cache = fopen( $path_to_html_file, "w" ); // will create a new file, or open and overwrite it if it already exists

flock( $new_cache, LOCK_EX ); // locks file to prevent errors when writing
fwrite( $new_cache, $html ); // writes html from $html to file
flock( $new_cache, LOCK_UN ); // unlocks file
fclose( $new_cache ); // closes file

?>

Link to comment
Share on other sites

That wouldn't do any good. The client would still have to download those pages. They would just be pre-generated. Which is bad. It means they aren't dynamic, which means you shouldn't even be using PHP.

Link to comment
Share on other sites

PHP caching over a 1 hour timeout:

<?php

$page = '/path/to/cache/page/file.html';

if (file_exists($page) && ((time() - filemtime($page)) < 3600))
{
    echo file_get_contents($page);
}
else
{
    ob_start();

    /**
    /* create the page here..
     */

    file_put_contents($page, ob_get_contents());

    ob_end_flush();
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.