开发者

How to cache specific parts of page in PHP intelligently?

Problem: I've a website which has 50k+ pages and most of them are only updated once on the creation time, I am building a caching for these pages as they do not need any frequent changes on the content. But I'm confuse about few things.

  • If I cache whole page, how do i preserve User Login/Logout status in sidebar
  • How do I cache the meta tags for the page as main caching is done in the middle of page as this is most expensiv开发者_开发百科e part as far processing is concerned.


Take a look at the Cache_Lite PEAR module.


For your first point - you can make the login status sidebar load via AJAX, if you want to cache the rest of the page. Since the result of the AJAX request is separate (and presumably, not cached), it will update properly.

Assuming you're actually sending the meta tags as part of the page content, you might want to wait to actually write anything to the output stream until after all of your processing is done, so that you're free to compute things in any order, even if they come earlier or later in the actual page HTML content.


Obviously I don't have specific information regarding your setup, but a common tactic is to simply cache the database result/page specific content in Memcached. If Memcached isn't an option for you, you could create a writable directory on your server and just cache the page specific content there. So, you would still be generating the user specific content on each request, but without the overhead of querying the database unnecessarily.


Apply content filters before output.

You could simply use parts in your template that are reference ids of content that should be dynamic, and replace them with a the result of a function call, on the fly.

Example:

<p>some cached content</p>
<div id="user_box"> {{USER_BOX}} </div> 
<p>other cached content</p>

First, fetch your cached content, then in your code, replace {{USER_BOX}} with a function call result, and finally output the modified result.

This way, you won't need to code and perform extra AJAX requests (the more server calls the less performance).

Of course you could treat your cached content with some template engine like Twig (part of Symfony) if you're using php. That would add some extra features like conditions, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜