/    Sign up×
Community /Pin to ProfileBookmark

Dynamic content caching for performance

Hi,

This is not a problem with a specific page but it is something I have come across on many sites I have worked on with and with different technologies. I would appreciate your opinions or links to pages that discuss the issue since I have not been able to get relevant search results.

I am interested in caching a dynamic page on the server rather than recreating it on every access. I know that sounds like defeating the purpose of dynamic pages but I think there is value for it. And I don’t mean caching for a user in the event of revisiting the page. I want to serve a cache copy to all users.

Think of a high traffic site, say 100+ hits per second and the home page including 15 queries to the database. The user cannot post to this site so it cannot tell whether a copy is current or 5 seconds old. So if the content was generated every 10 seconds and served to all users as static content it would save tens of thousand of database queries which may not all be fast.

With Servlets I could have a thread generating the content and saving it in a String and just printing it out for every GET request. This does not seem as simple with PHP.

Some solutions come to mind, such as having a process generating the page and adding it to the database to be retrieved by a single very fast query. Are such techniques being practiced?

I am looking for “best practices” or “tried and tested” ways of doing this. I could not find anything relevant by searching google but this would be very useful for certain sites. Any thoughts would be appreciated.

Thanks
yiannis

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 06.2006 — You really need to think about what you are caching and why. If you are just looking to save the load on the DB you could cache the query results to a file and use them until you consider the stagnant. This approach would allow you to refresh some parts more frequently than others. Another appraoch would be cache the whole page. This is easy and only requires buffering so a copy of the page can be saved. Lastly you could send 304 responses when the page is cached and unchanged.
Copy linkTweet thisAlerts:
@Sid3335Nov 06.2006 — i've been reading about a web proxy cache called squid:

http://www.squid-cache.org/

might be easier all round.
Copy linkTweet thisAlerts:
@yiannisauthorNov 06.2006 — You really need to think about what you are caching and why. If you are just looking to save the load on the DB you could cache the query results to a file and use them until you consider the stagnant. This approach would allow you to refresh some parts more frequently than others. Another appraoch would be cache the whole page. This is easy and only requires buffering so a copy of the page can be saved. Lastly you could send 304 responses when the page is cached and unchanged.[/QUOTE]

Is there a simple way in php to cache the page for you or do I have to build it? Can you give me a link to more info?

An example of what I am talking about: I worked on a sports site that included a lot of info about results, schedules, top scorers and other info which made getting the webpage very slow especially since all users seemed to visit at the same time (saturday evening after the games to check out the results). A lot could be done at the database level but generating the page in a java thread (site was using Servlets) and printing out the string for each GET request completely solved the problem, the site was pretty fast after that, the solution took only a few hours to implement. The timeout was really tiny, just a few seconds (so no point worrying about 304 or refreshing parts of the page at different intervals) but was enough to make a huge difference since it had to go to the database just a couple of times rather than hundreds/thousands of times during those few seconds.

So I came up with that Java solution and would like to have a PHP counterpart for some other sites I am working on. I guess I could write to plain files or add html strings in the database but was looking to read discussions/guides on this issue since I don't want to re-invent the wheel or do something potentially unsafe or error-prone.

Is this not an issue the serious sites out there are facing ? Has this not been addressed by the industry? It would seem odd... then again there may be better approaches....


thanks
Copy linkTweet thisAlerts:
@bokehNov 06.2006 — You'd need to write it yourself. I haven't seen any good tutorials about this but it would be very simple. Is your site using a query string or just a plain URL? If you want I could write you a quick example script.
×

Success!

Help @yiannis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...