Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling caching dynamic data

Tags:

php

caching

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 certain 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.

like image 496
AKor Avatar asked Mar 19 '26 21:03

AKor


1 Answers

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.

like image 194
Dave Wise Avatar answered Mar 21 '26 09:03

Dave Wise