English 中文(简体)
PHP - Memcache - HTML Caching
原标题:

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,

最佳回答

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.

问题回答

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.

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签