PHP-Memcache-HTML缓存 [英] PHP - Memcache - HTML Caching

查看:70
本文介绍了PHP-Memcache-HTML缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个缓存系统,该系统将绕过某些机制以提高性能.

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

我有一些例子:

1-)我有一个动态的PHP页面,该页面每小时更新一次.每个用户的页面内容都是相同的.因此,在这种情况下,我可以:

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)创建一个HTML页面,该页面可以每小时生成一次.在这种情况下,我想绕过PHP,因此应该有一个静态页面,并且如果数据库已更新,将生成一个新的HTML文件.我怎样才能做到这一点?我可以创建一个生成HTML文件的crontab脚本,但这似乎不是一种优雅的方法.

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)将输出缓存在内存中,因此Web服务器将每小时更新一次内容.我想我需要Web服务器的内存缓存模块.有一个针对lighttpd的非正式的Memcache模块,但它似乎不稳定,我也听说过针对Nginx的Memcache模块,但不知道这是否可能.这种方式似乎更优雅,更可行,但是如何呢?有任何想法吗? (再次,在这种情况下,我想绕过PHP)

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)

另一个示例是我有一个动态PHP页面,该页面每小时更新一次,在该页面中,只有用户详细信息部分是完全动态的(因此,用户登录或注销并在该部分查看其状态)

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)

再次,如何为该页面创建缓存系统?我认为,如果我可以找到第一个示例的解决方案,那么可以在该部分中使用AJAX并使用相同的解决方案.我说得对吗?

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?

我想,我不清楚.我想完全绕过PHP. PHP脚本将每小时运行一次,此后将不进行任何PHP调用.我想消除它的开销.

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.

预先感谢

推荐答案

使用静态HTML.每小时仅需使用输出更新静态HTML文件即可.您将需要使用每小时 cron 来运行PHP脚本,以 fopen() fwrite()到文件.无需点击PHP即可检索页面.只需为该特定页面制定.htaccess mod_rewrite重定向规则即可维持您当前的URL命名.

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.

尽管不是很优雅,但是对我来说,带有 gzip压缩静态HTML 效率更高,并且会使用更少的带宽 >.

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

使用cron每小时运行一个PHP脚本的示例:

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

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

输入以下值:

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

最后一部分确保您没有任何输出.这个cron将在每小时的第一分钟运行.

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

更新

要么我错过了有关您的动态用户个人资料信息的部分,要么是在我的初始评论之后添加的.如果您仅使用单个服务器,建议您切换到APC,它提供操作码缓存和缓存机制的速度比memcached(对于单个服务器应用程序)要快.如果用户的个人资料数据在折叠以下 (在用户的窗口视图以下),则您可能需要等待AJAX​​请求,直到用户向下滚动到指定点为止.您可以在Facebook状态页面上看到此功能.

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.

这篇关于PHP-Memcache-HTML缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆