0

I'm a little confused here.

I know that for every PHP request, the entire application is bootstrapped all over again.

Given this, how can a cache be effective, if all of the globals are reloaded for each and every request?

For example:

User calls URI/user/view/123. User 123 is loaded from a database and stored in $user.

Why would you cache the contents of $user - when you merely need to refer to the variable in order to get the contents?

Am I missing the point?

Thank you,

Sam Levin
  • 3,326
  • 7
  • 30
  • 44

2 Answers2

2

Its more like caching images, common database querys

For instance say your site has a lot of articles and each article has categories. And say you dont change categories very often, then using a cached result of a query of the categories table is preferable then doing the query. this is a simplified example.

Another example is with images, if your site needs like thumbnailed version of user photos that they have uploaded instead of having php use the GD library to rescale the image and etc just save a version of that thumbnail version and use it instead of running through the GD code again.

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • Another type of caching for PHP specifically is caching the semi-compiled code. This avoids recompiling the PHP code for every request. See this answer: http://stackoverflow.com/a/5377415/1180785 – Dave Jun 02 '13 at 14:51
0

As always, an image is worth a thousand words, here it is :)

enter image description here

(source)

As you can see, you reload some PHP librairies (like the basic environment (Globals, Requests, Cookies, etc), but not everything (in this case, Security, Application, various libraries, Views).

You skip what can be cached ;)

Cyril N.
  • 38,875
  • 36
  • 142
  • 243
  • though i think the security part should be before going to the caching :P – Patrick Evans Jun 02 '13 at 14:54
  • The diagram refers only to full page caching - thats not the most common, or in most cases the most appropriate. – AD7six Jun 02 '13 at 14:57
  • I know, but it's an example. there is a various way to cache something (heck, even saying this is too broad). You can cache the whole page (like the example I gave) up to only cache the sql queries (for example, even more directly in the database). – Cyril N. Jun 02 '13 at 14:59
  • @PatrickEvans I agree with you, but I believe it depends on the page you are showing (and this example doesn't work if it's in a member area for example) – Cyril N. Jun 02 '13 at 15:00
  • If an in-memory cache (referenced by a global handle) is reloaded for every PHP request, how would you effectively cache a completely static page? You would have to load the page to cache it, and the cache would be reset on every request (unless using memcached, which I do not plan to use for quite some time). – Sam Levin Jun 02 '13 at 15:08
  • If your page is completely static, you should avoid using PHP for that, but instead, use directly Apache/Nginx/... . Now, you won't be able to fully cache a PHP page and avoid loading a small core of yours scripts. It's exactly the point of a scripting language. – Cyril N. Jun 02 '13 at 17:44
  • You can also improve your code by compiling it (look at APC for example) but some php code will always be loaded. – Cyril N. Jun 02 '13 at 17:45
  • Cyril - but I'm using an MVC-type script, where one of the routes merely leads to a static page. How can I just completely disregard my app to load that one page? – Sam Levin Jun 02 '13 at 19:08
  • It's hard to tell with so little details on the project (which I understand), but a possibility would be to define an .htaccess that would have the same regex than your Router for the static content, and show them directly with no interpreter (take a look at Prestashop, they also does this) – Cyril N. Jun 03 '13 at 09:45