开发者

Handling caching dynamic data

I'm using something called Quickcache for PHP that provides full-page caching of webpages. It works great, but my only issue is that I have a dynamic website, and certa开发者_如何转开发in things do not update the way I want them to when caching is turned on.

For example, if a user logs in, the login box will still appear, even though to the server the user is logged in (I can go to an uncached part of the site and it'll show the user logged in perfectly fine). Same goes for changing user settings, etc.

I also can't dump the cache every time a user logs in or changes a tiny setting, that'd be overkill.

It seems like I can't cache because it makes the site unusable, and I don't cache the site will be unusable under any appreciable load.

How should I solve this problem? I'm pretty new to caching in general - I'm rolling out one of my sites for production for the first time. Surely others have had and solved this problem.


I'm not a php person but I can tell you about caching.

Dynamic sites that generate content per user are the trickiest to do effectively, however, it can be done. It will require that you look at how data flows in your application in order to determine how, where and what to cache. Here are some guidelines :

  • Data that does not change per user or per page - cache in the application memory and grab it instead of going to the DB.
  • Data that changes per user but not per page - cache in the user session
  • Data that changes per page but not per user - cache in app memory using the page name as the key
  • Data that changes per user per page - cache in session with page name as key
  • Data that is unique per page request - do not cache.

Not just data from the database is a candidate for caching. If you have a block of complex logic that manipulates data, consider caching the output of that logic.


You'd be lucky to have so much traffic to your site right away, wouldn't you?

Not sure how this particular tool "Quickcache" works, but conceptually what you want to do is have a top-level PHP file with includes for the parts that can be cached, so that you load the semi-static parts from cached copies and the top level doc containing dynamic content on each load. Smarty allows you to do this with templates I believe. For simple cases (and some not so simple) I usually write my own code.

Then clear the cache on the include files only as needed, daily by cron perhaps or triggered in the code by an administrative update which changes values there.


Library you use has poor docs, I couldnt find anything good.

But I would suggest making your own caching system using for example Zend_Cache lib (if youre using this framework). You will decide what to cache and how to cache, so thats basically the best effect you can achieve.

After page loads, you will check for the page in the cache, and if its there, use it.

This is the best way I can suggest, as you dont want to cache some dynamic actions (routes). You can simply exlude them, from caching system you will make. Im doing similar things with complex queries, but you can cache everything. And just after you change sth on page you can refresh the cache.


Sorry to revive an old post, but after reading this I feel that the OP question is still very relevant.

From what i`ve gathered, what you have here is a personalisation and freshness issue, very common problem when dealing with dynamic sites.

What you need to do is :

A. Monitor content usage to identify dynamic objects that were constantly re-served (but not Cached). Note that identifying absolutely (100%) unchanged objects will help avoid personalisation related issues, so you need to make sure that you are using cross-verification, rock solid methods... (i.e. check Size, down to the byte)

B. Once good "potential candidate" located, make it Cachable by overriding/creating new HTTP directive for Caching.

C. Continue monitoring content usage, reloading fresh version from time to time (often) and comparing it to the currently Cached object, to keep it fresh.

This is the gist of it. A description of a similar "learning" Caching mechanism can be found here: Dynamic Caching.

The one inside used similar learning heuristics to Cache dynamic content and 5 min refresh rates for freshness.

GL.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜