Results 1 to 11 of 11

Thread: PHP Cache?

  1. #1
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122

    PHP Cache?

    Is it possible to cache around 500,000 pages? Each page is around 15kb~

    My database isn't running as fast as I need it to be. :/

    I've never really cache'd anything, so I'm not sure if this is a crazy idea. Or even possible.

  2. #2
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122
    Been doing a bit more reading, seems like some sort of cache system is standard for any large site

    So is it basically creating a script to create hundreds of thousands of pages on the server , there's no alternative that Apache has built in or anything (?)

  3. #3

  4. #4
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122
    Yeah, I have/had read that. It's just the issue of keeping hundreds of thousands of static files makes me slightly uneasy.

  5. #5
    Not that blue at all Blue Cat Buxton's Avatar
    Join Date
    May 2004
    Location
    UK
    Posts
    932
    Quote Originally Posted by ZigE View Post
    Yeah, I have/had read that. It's just the issue of keeping hundreds of thousands of static files makes me slightly uneasy.
    Why? Because they will become outdated or just because of the additional space on your server?

  6. #6
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122
    Neither particularly. Just trying to do some due diligence, so I don't cause myself any unneccesary headaches

  7. #7
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    There is a second caching article on this site too, which is what I actually use for my literature site.

    http://www.websitepublisher.net/arti...-php-database/

    Honestly, the storing large number of files is immaterial, use an appropriate directory structure and your site will run much faster.

    The difficulty is in regularly rewriting those files. I do it once a week, but I'm not doing half a million pages. You may want to code it so you can only do it when the content is updated.

    In general though, caching like this is a great idea for any database driven site that does not rely on frequently updated (aka user contributed) content.

    In the end, running the script to generate the files is not more difficult for your server than having someone visit your website and request each page. Just, it'll be happening all at once, my recommendation is to code a sleep() into the php script to give it a little rest at certain intervals.

    The other benefit, is when you cache, you can have more flexibility to use really powerful, complex, or inefficient SQL queries on your pages you're caching because rather than running on every page view, those queries are now just run once a day/week/month whatever.
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  8. #8
    4x4
    Join Date
    Oct 2004
    Posts
    1,043
    I use a script that generates a new cache version the first time it is selected.
    Then at 1am all cache files are removed.

    So you may have 500k pages but if not all are accessed each day then not all are created either.

    Obviously if your site is doing 500k+ page views per-day then re-writing each day and deleting daily may be a bit much unless your content needs it.

  9. #9
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122
    Thanks. Yeah Todd the content only really updates every month or so. But theres still some dynamic parts of the page that need to be kept dynamic, (show different ads, depending on where the user came from.)

    So i'm thinking of creating an initial cache, then somehow inserting bits of dynamic code into the page. And saving the page again

    hmm

  10. #10
    Registered ZigE's Avatar
    Join Date
    Dec 2006
    Posts
    122
    Ok, so using the cache article on this site, how exactly do you parse dynamic urls through the function

    Like articles.php?id=3

    Code:
    function get_include_contents($filename) {
       if (is_file($filename)) {
    	  
           ob_start();
    	   
           include $filename;
           $contents = ob_get_contents();
           ob_end_clean();
           return $contents;
       }
       echo "no file";
       return false;
    }
    
    $string = get_include_contents('/home/site/public_html/articles.php');

  11. #11
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    A couple ways really.

    Code:
    $id = 3;
    
    function get_include_contents($filename) {
       if (is_file($filename)) {
    	  
           ob_start();
    	   
           include $filename;
           $contents = ob_get_contents();
           ob_end_clean();
           return $contents;
       }
       echo "no file";
       return false;
    }
    
    $string = get_include_contents('/home/site/public_html/articles.php', $id);
    Get it? Here is how I do it with my literature site. I pull a list of authors from my database and then...

    Code:
    while($main = mysql_fetch_array($result1)){    
    $author = $main[author-id];
    
    $sidebar = get_include_contents('authorsidebargen.php', $author);
    
    
    $handle = fopen("/home/lit/public_html/$author/sidebar.php", "w");
    
    if (fwrite($handle, $sidebar) === FALSE) {
           echo "Cannot write sidebar for ($author)";
          
       }
       
       echo "Success, wrote sidebar for ($author)";
       
       fclose($handle);
    
    
    
    
    $string = get_include_contents('../../authorgen.php', $author);
    
    
    
    
    
    $handle = fopen("/home/lit/public_html/$author/index.php", "w");
    
    if (fwrite($handle, $string) === FALSE) {
           echo "Cannot write index for ($author)";
          
       }
       
       echo "Success, wrote index for ($author)";
       
       fclose($handle);
    
    }
    
    function get_include_contents($filename, $author) {
       if (is_file($filename)) {
    	  
           ob_start();
    	   
           include $filename;
           $contents = ob_get_contents();
           ob_end_clean();
           return $contents;
       }
       echo "no file";
       return false;
    }
    I actually write two files for each, a sidebar, which is the left menu, and an index page, which is the normal index page + the left menu. I do this because the left menu appears on pages I cannot cache, like search result pages, so by caching it I'm also lowering the resource usage of those search result pages. Meaning, the only query done on the search pages is the search query, no standard page content queries are necessary.
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. Super Cheap PHP 5, RoundCube Webmail Linux Shared Hosting
    By hostingatoz in forum The Marketplace
    Replies: 0
    Last Post: 08-04-2007, 01:00 AM
  3. Replies: 0
    Last Post: 05-24-2007, 04:48 AM
  4. Using PHP to produce PHP really screws with the head.
    By KLB in forum Website Programming & Databases
    Replies: 8
    Last Post: 02-14-2007, 11:36 AM
  5. php: cache files on a remote server ?
    By tomek in forum Website Programming & Databases
    Replies: 2
    Last Post: 07-12-2004, 08:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •