Full Page Caching
  • January 2010
    With all the talk about Googles latest algo changes to include page load times and magento adding page caching in it latest upgrade it looks like software developers are starting to act.
    Full-page caching is the simplest and most straightforward dynamic caching mechanism. The user defines which page to cache and the server stores it in the cache disk space. In subsequent request for the page, instead of parsing the page, running the script and building the HTML page, the server will locate the existing cached page and send it to the client. The home page for a Web site is an excellent example of a popular page for full-page caching. Generally it is the first page that users see, the most frequently accessed and therefore has the greatest impact on the user experience. As mentioned, when using dynamic content caching the web server no longer creates pages for each request, but sends pre-built copies to the clients. Therefore, users do not see content changes once a page has been cached. Introducing the concept of 'lifetime' settings can solve this problem while still maintaining huge performance gains, even when set as low as two minutes. Lifetime settings permit the server to discard cached pages once their defined lifetime has expired, forcing the application server to reprocess the pages.
     
  • January 2010
    Thinking about it in simple terms this is getting a dynamic server to replicate a server with static html pages so gaining the advantage of that type of site.

    This will kill off one of the last benifits of having a site built from static html pages IMO
     
  • January 2010
    if anyone is interested in creating a web app that caches, let me know.

    i have done this many times in php, and the code is all yours, should you need to add full page caching to your app.
     
  • January 2010
    its as simple as this in php:

    Create a file called "cache_header.php" containing this:
    <?php

    $starttime = microtime();
    $startarray = explode(\" \", $starttime);
    $starttime = $startarray[1] + $startarray[0];

    // Settings
    $cachedir = 'cache/'; // Directory to cache files in (keep outside web root)
    $cachetime = 600; // Seconds to cache files for
    $cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)

    // Ignore List
    $ignore_list = array(
    'domainname.co.uk/ignorethis.php',
    'domainname.co.uk/ignorethisaswell.php'
    );

    // Script
    $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
    $cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create

    $ignore_page = false;
    for ($i = 0; $i < count($ignore_list); $i++) {
    $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
    }

    $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
    @clearstatcache();

    // Show file from cache if still valid
    if (time() - $cachetime < $cachefile_created) {
    ob_start('ob_gzhandler');
    @readfile($cachefile);
    ob_end_flush();
    exit();

    }

    // If we're still here, we need to generate a cache file

    ob_start();

    ?>


    Then create a file called "cache_footer.php" containing:
    <?php

    $endtime = microtime();
    $endarray = explode(\" \", $endtime);
    $endtime = $endarray[1] + $endarray[0];
    $totaltime = $endtime - $starttime;
    $totaltime = round($totaltime,5);

    echo \"<!-- This page took $totaltime seconds to generate -->\";

    // Now the script has run, generate a new cache file
    $fp = @fopen($cachefile, 'w');

    // save the contents of output buffer to the file
    @fwrite($fp, ob_get_contents());
    @fclose($fp);

    ob_end_flush();

    ?>


    in the same folder, create a folder called 'cache' and set it to writable by the php, using an FTP client:
    image

    Finally, implement the cache on your dynamic page:
    <? require('cache_header.php'); ?>

    <?
    //this is dynamic php
    ?>

    <? require('cache_footer.php'); ?>


    This effectively creates static html of a dynamic file and saves it to the cache. The cache is used if the same url is requested within a certain timeframe. This means that database intensive popular pages load instantly.

    good eh?
     
  • January 2010
    the real problem with this system is that it does not pay any attention to pages that are dynamically different due to logins and sessions.

    dont randomly implement this on any old php software. this is a bare bones example. In order for this to work in a complex example where individuals with different credentials are likely to see different pages, you'd need to have the system aware of their credentials, and display a fresh version of the page, based on the presence of an individual. This is NOT covered in the example, and it assumes that every user gets the same page.

    :)
     
  • January 2010
    Brilliant gabe:woot:
     
  • January 2010
    What I tend to do, is leave the main page uncached, and any elements that are custom to the user and experience uncached.

    The included elements i will cache. For example, my popular products block, i will cache. When they are pulled into the page by the server, the server merely includes a file, instead of drawing from a database.

    Overall this approach isnt as speedy as caching the entire page, but its very close and as a performance versus benefit tradeoff, it works very well indeed.
     
  • January 2010
    We just now need to get isc to build this in as standard the same way magento has.