PHP Caching Unplugged

LAMP (Linux, Apache, MYSQL, PHP) stack is used by many of the web applications globally and more so by the startups. Most popular social network Facebook is also built using PHP. This article explains how caching can be implemented at various levels in PHP application. Caching is practice of trading space (memory or Disk) for extra speed. Basically caching is mechanism of storing processed data so as to avoid re-processing later. Caching is more value adds especially when data changes less frequently and its time consuming to process the data.

There are different caching mechanisms available in PHP. They are:

  • Script Caching: The PHP engine which parses PHP code and translates it into a byte code, then interprets the byte code. In PHP the source code file is reparsed for every request made to the server, even if no changes have been made since the last time it was parsed. Script Caching saves the byte code to a file (known as compiling), so that the source code does not have to be reparsed if it hasn’t changed. Parsing can take a substantial amount of time on each request. For many scripts, the time required to parse the PHP code into byte code will take longer than the time need to interpret it. There are number of Script Caching Engines, one such engine is Alternative PHP Cache(APC). APC is simple PHP addon and easy to use.
  • Template Caching: Template caching occurs when you’re using a template engine such as Smarty . Given that a template for a page will only change infrequently (when a modification is made to the look and feel of that page), it’s important not to “interpret” the template at runtime, given the overhead of this process. More advanced template engines will compile a template into native PHP code and cache it as a file for re-use until the original template changes. Such a caching mechanism will typically be part of the template engine and implemented in a form so that users of the template engine need not be concerned about it’s impact on their application design.
  • DB Query Caching: If your system is DB intensive and data is changing less frequently then DB Query Caching is solution for you. Basically DB Query Caching can be done in two ways. One way is file caching where the output of DB query is stored in file in some format and when the request comes back instead of firing a DB query we do file parsing. But file parsing will consume some time and if file parsing is implemented inefficiently it may back fire. Second way is to use memcache, The output of DB query can be stored in RAM using memcache. But if the output of DB Query is huge then you may run out of memory if you are using memcache.
  • PHP Output Caching: In PHP output caching you will be caching the output of the entire PHP program. Even here you have two options File and memcache. File is better option here as you will be saving the RAM and also unlike DB Query Caching this does not need file parsing.
  • Client Side Caching: Client Side Caching is nothing specific to PHP but article wouldn’t be complete if we don’t mention it here. Client Side Caching will inform browser for how long to serve a page from browser cache. Scripts, therefore, need deal with HTTP caching headers correctly. A simple but effective approach, for most situations, is to work only with the HTTP 1.0 headers “Last-Modified” (response header) and “If-Modified-Since” (request header), as well as the “304 Not Modified” status code. As HTTP cache headers work on a page by page basis, the script handling these headers needs to know the age of the content it’s rendering. In practice this form of caching works best in conjunction with output caching (above).

Whatever may be the caching approach you are following, you should appropriately expire the cache when it’s required, for example you need to expire DB Query cache whenever the output of the DB Query changes.

Was this article helpful? Please let us know your feedback.

No comments:

Post a Comment