• 0

Cache Script


Question

umm sorry if theres a typo in the title :p

but i made a script (im quite happy with it - my first GD script :D)

and i want it to chahce the data and only refresh it once every 30mins any idea on howto do this?

script:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/png");
include("pulse.class.php");
$im = imageCreateFromJpeg("whatpulse.jpg");

$whatPulse = new pulseData;
$user = "Trix";                           
$raw = $whatPulse->getPulse($user);
$data = $whatPulse->parsePulse($raw);

$total = $whatPulse->grabVar($data, "TotalKeyCount");
$rank = $whatPulse->grabVar($data, "TeamName");
$teamrank = $whatPulse->grabVar($data, "RankInTeam");
$teamname = $whatPulse->grabVar($data, "TeamName");
$clicks = $whatPulse->grabVar($data, "TotalMouseClicks");

$black = ImageColorAllocate($im, 252, 191, 12);

// ($im, text size, x-coor, y-coor, text, color)
    ImageString($im,2,20,5,"Username: Trix",$black);
    ImageString($im,2,111,5,"Keys:" . $total ,$black);
    ImageString($im,2,197,5,"Clicks:" . $clicks ,$black);
    ImageString($im,2,294,5,"Team:" . $teamname ,$black);
    ImageString($im,2,397,5,"Team Rank:" . $teamrank ,$black);

imagepng($im);
imagedestroy($im);
?>

pulse.class.php ( i stole this from somone :shifty: ):

<?
//snip file info - takes up to much space on the preview

class pulseData
{

  // Grab pulse data. //
  function getPulse($user)
  {
    
    $fp = fopen("http://whatpulse.org/api/user.php?account=$user", "r");
    $data = fread($fp, 4096);
    $data = explode("\r\n", $data);
    
    fclose ($fp);
    return $data;
  
  }
  
  // Parse and sort returned data. //
  function parsePulse($data)
  {
    foreach($data as $key => $line)
    {
      if(!ereg("^(\#|\X)", $line) && $line != "")
      {
        $buf = explode("W ", $line);
        $con = strpos($buf[1], " ");
        $var = substr($buf[1], 0, $con);
        $val = substr($buf[1], $con);
      
        $results[$var] = $val;
      }
    }
  
    return($results);
  }
  
  // Grab individual value. //
  function grabVar($data, $var)
  {
    $tvar = $data[$var];
    return ($tvar == "" ? "Invalid Parameter!" : $tvar);
  }
}

?>

any ideas? common, i now we have loads of PHP Gurus here! :D

any help is appreciated

Trix

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

well more or less you want to make another script that instead of outputting the .png to the browser, saves it to a file.

php manual on imgpng() will tell you how to do this (www.php.net/manual)

then, you'll want to set up a cron job (if your webserver supports that) that runs the script that generates the png every 30 mins.

this is the commandline item you want

curl http://yourdomain.com/yourscript.php

if you don't have access to cron jobs, you can write a "gateway" type script which does something like:

first, check if the file result.png exists

no: generate it, save it, output the png to the user, update lastgen.txt

yes: check lastgen.txt, see when result.png was last updated, if it's more than 30mins ago, generate the png, save it, output to browser

if it wasn't more than 30 mins, output the result.png to the user

cron job is perhaps easier, though technically less efficient if, for example, you don't need it to be updated during the middle of the night when people are sleeping.

generating it probably doesn't generate much load though.

Link to comment
Share on other sites

  • 0

I posted this a while ago, it is snippet of my new SIG system which i am finishing - But i'll post it again ;).

If you make your script write to a file - sig.txt for example - when it finishes loading you can check the next time when the stats were last updated.

Then you can use this...

<?php

   $modified = stat("sig.txt"); 
             
    if($modified[9] < (time() - 1800)) {
// Put code here to update it
} else {
// Do something else, pulse data is under 30mins old.
}

?>

... to check the sig.txt file and update where necessary.

Hope this helps.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.