• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Cache Script

Question

trix    1

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

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
ike    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.

Share this post


Link to post
Share on other sites
  • 0
trix    1

ok =) and yeh i have access to cron jobs (i think) on ASO i'll see if i can do it the way you've said :)

Trix

Share this post


Link to post
Share on other sites
  • 0
trix    1

gah. beyond my scope >_>

if anybody could do this for me i would greatly appreciate it :D

Trix

Share this post


Link to post
Share on other sites
  • 0
Zero1    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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.