SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php caching for frequently changing sites

    I've been looking into caching page output, so I don't have to process the page every time it's requested. This works great so far on the pages that don't change too often, but I was wondering what might be some good ways of handling a page, or multiple that change frequently. Specificly, I'm making a game and so the pages will be changing very frequently.

    So far my idea is to just add a small txt file for everyone that logs in, then whenever they do something that changes anything on one of the pages, or when someone else does something that effects a stat of the first player, it just saves a timestamp in the txt file. Then I would just use this timestamp in the "key" of the caching program so that if the number changes it won't keep using the old version.

    Has anyone done this or something similar? Is the time saved worht the effort and overhead?

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  3. #3
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I must say that I have never actually done anything like this but I belive I know of a way.
    It uses the ob_ functions
    http://is2.php.net/manual/en/ref.outcontrol.php

    Anyway, as I would do it ...

    You decide a name for the cache file for each page on your site, then at the beginning and end you do the following:
    PHP Code:
    $cacheFilename 'somename.txt';
    if (
    file_exists($cacheFilename)) {
      
    $handle fopen($cacheFilename'r');
      
    $content fread($handlefilesize($cacheFilename));
      echo(
    $content);
      exit();
    }
    ob_start();

    //do anything the page is supposed to do

    $content ob_get_contents()
    $handle fopen($cacheFilename'w');
    fwrite($handle$content);
    ob_end_flush(); 
    As I said before, I have not done this but it should work.

    And then when you know that the sites need to be updated I would simply delete the cache file (take a look at unlink()), no need to store a timestamp or anything.

    Hope that helps!
    - website


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
  •