?
Solved

Cron Jobs To Create Pseudo-Static Pages With PHP

Posted on 2006-05-21
9
Medium Priority
?
359 Views
Last Modified: 2010-04-22
I have a php page that is query intensive.  I would like to create a cron job that runs every 5 minutes or so that takes the PHP file as input, and outputs a static HTML file to decrease server load.  I have NO idea how to do this.
0
Comment
Question by:WarriorPoet42
  • 5
  • 3
9 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 16731441
Create a cronjob  along the lines of

*/5 * * * * /path/to/script.php >/path/to/static.html
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16731917
In fact I would not use the cron job for that purpose.
I would incorporate caching mechanizm in the php script itself.
On run, it could check the cache when it was generated last time. If too long time ago, regenrate the cache. After the check, serve the webpage from the cache.
The cache itself may be in another file (like script.php.html) or shared memory or even database.
0
 

Author Comment

by:WarriorPoet42
ID: 16732994
How would that be coded, ravenpl?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
LVL 43

Expert Comment

by:ravenpl
ID: 16733047
example for file based

<?php
$filename = "??";
$now = time();
$fnow = filemtime($filename);
if ($now === false || $fnow < ($now - 300)) #300 second
{
  require("lib/regerate_filename.php");
};
readfile($filename);
?>
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16733056
For to avoid regerating from two requests at same time
http://pl.php.net/manual/en/function.flock.php

Also, it does not have to be file, You can store the current pageView in the database (separate table with just one entry pair<lastModified, content>)
0
 

Author Comment

by:WarriorPoet42
ID: 16734365
I'm not sure I understand what your script is doing.  If the file I want to cache is called players.php would that go $filename = "players.php"; ?

What would regenerate_filename.php be?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 16734499
Move players.php to lib/ directory. (I assume one can't access files in lib/ directory)
Change players.php, so it writes it's output to say cache/players.txt.
Create new players.php

<?php
$filename = "cache/players.txt";
$now = time();
$fnow = filemtime($filename);
if ($now === false || $fnow < ($now - 300)) #300 second
{
  #make sure we'll not generate it concurrently
  $fp = fopen($filename, 'a');
  if(flock($fp, LOCK_EX)) require("lib/players.php");
  fclose($fp);
};
readfile($filename);
?>
0
 

Author Comment

by:WarriorPoet42
ID: 16734705
How would I make the php file output into a text file?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16735329
open the file and print to file instead of stdout, but
to not mess with the original players.php You could use folowing wrapper(give feedback if it works, as I haven't tested that)

<?php
function ob__callback($buffer)
{
 fwrite($_GLOBALS["fp"], $buffer);
 return $buffer;
};

$filename = "cache/players.txt";
$now = time();
$fp = 0;
$fnow = filemtime($filename);
if ($now === false || $fnow < ($now - 300)) { #300 second
  $fp = fopen($filename, 'w'); #truncates as well
  flock($fp, LOCK_EX); #ignore errors, we already truncated the filename
  ob_start("ob__callback");
  require("lib/players.php");
  ob_end_flush();
  fclose($fp);
} else {
  readfile($filename);
}
?>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses
Course of the Month4 days, 16 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question