views:

462

answers:

3

Hi,

I would like to create a caching system that will bypass some mechanisms in order to improve the performance.

I have some examples:

1-) I have a dynamic PHP page that is updated every hour. The page content is same for every user. So in this case I can either:

a) create an HTML page, and that page can be generated every hour. In this case I would like to bypass PHP, so there should be a static page and if the database is updated, a new HTML file will be generated. How can I do this? I can create a crontab script that generates the HTML file, but it does not seem as an elegant way.

b) cache the output in the memory, so the web server will update the content every hour. I guess I need a memory cache module for the web server. There is a unofficial memcache module for lighttpd, but it does not seem stable, I have also heard a memcache module for nginx but don't know whether is this possible or not. This way seems more elegant and possible, but how? Any ideas? (Again, I would like to bypass PHP in this case)

Another example is that I have a dynamic PHP page that is updated every hour, in that page only user details part is fully dynamic (so a user logs in or out and see his/her status in that section)

Again, how can I create a caching system for this page? I think, if I can find a solution for the first example, then I can use AJAX in that part with the same solution. Am I correct?

edit: I guess, I could not make clear. I would like to bypass PHP completely. PHP script will be run once an hour, after that no PHP call will be made. I would like to remove its overhead.

Thanks in advance,

A: 

If this is just a single web server, you could just use PHP's APC module to cache the contents of the page. It's not really designed to cache entire pages, but it should do in a pinch.

Edit: I forgot to mention that APC isn't (yet) shipped with PHP, but can be installed from PECL. It will be shipped as part of PHP 6.

R. Bemrose
This, or memcached, and just have PHP retrieve whatever value you need. Also, there's probably no need to bring AJAX into the picture for user data unless that needs to be all sexy and dynamic; PHP is actually pretty good with strings, so just loading the static portion of the page and slapping in user-specific data should be pretty easy.
Dereleased
Thanks for the swift reply. I am already using memcached an opcode caching system, xcache. I like them, but I really do not use PHP for an hour, the data is updated once an hour. So I would like to bypass PHP completely , if possible. It will be run once an hour.
murat
A: 

A nice way to do it is to have the static content stored in a file. Things should work like this :

  • your PHP script is called
  • if your content file has been modified more than 1 hour ago (width filemtime($yourFile))
    • re-generate content + store it in the file + send it back to the client
  • else
    • send the file content as is (with file($yourFile), or echo file_get_contents($yourFile)

Works great in every cases, even under heavy load.

Nicolas
unless the file is particularly large, at which point `fpassthru` would be much better. See http://us.php.net/manual/en/function.fpassthru.php
Dereleased
thanks, but I would like to bypass PHP completely. I do not want to use PHP and remove its overhead. That's the problem.
murat
Then a crontab is the way to go, to call a script that will generate your HTML file(s), every hour for instance.
Nicolas
+1  A: 

Go with static HTML. Every hour simply update a static HTML file with your output. You'll want to use an hourly cron to run a PHP script to fopen() and fwrite() to the file. There's no need to hit PHP to retrieve the page whatsoever. Simply make a .htaccess mod_rewrite redirection rule for that particular page to maintain your current URL naming.

Although not very elegant, static HTML with gzip compression to me is more efficient and would use less bandwidth.

An example of using cron to run a PHP script hourly:

// run this command in your console to open the editor
crontab -e

Enter these values:

01 * * * * php -f /path/to/staticHtmlCreater.php > /dev/null

The last portion ensures you will not have any output. This cron would run on the first minute of every hour.

UPDATE

Either I missed the section regarding your dynamic user profile information or it was added after my initial comment. If you are only using a single server, I would suggest you make a switch to APC which provides both opcode caching and a caching mechanism faster than memcached (for a single server application). If the user's profile data is below the fold (below the user's window view), you could potentially wait to make the AJAX request until the user scrolls down to a specified point. You can see this functionality used on the facebook status page.

cballou
this doesn't address the second example; the suggestion of using AJAX complicates things by mixing the "overhead" (my god, milliseconds are being WASTED, people!) of PHP (unavoidable at this point) with a second request powered by javascript, which causes even more overhead to be brought into the equation. Having PHP parse a static file would probably be fine, but PHP from a memory caching device of some sort would be better. And anything you can do in one request is probably better than firing up AJAX; even an iFrame would be better -- this is not what AJAX ought to be for.
Dereleased
Yes, you are right about AJAX, but at least the page will be rendered very fast, the user details section is not important at this point. It can come 1 second later.Thanks,
murat