PHP - Memcache - HTML Caching

☆樱花仙子☆ 提交于 2019-12-04 19:10:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!