Thursday, 14 March 2013

Cache is King

One of the thing that I have noticed again and again is that most developers will write fantastically complex solutions. Then when the system starts to come under heavy load they then start to look at what tricks they can implement so that they can relieve the pressure on their servers.

Mostly you will see one or more locations where caching of some form or other is implemented into pages so what can you cache.  Normally you see Dynamic caching implemented.  Dynamic caching is when you cache the results of your page for say 30 minutes or an hour or even only for 5 minutes depending on your need for each page.  With Dynamic caching we still have the problem that when the age of the file is marked as too old we rebuild the complete page with the new content.  The problem is that on most web sites 90%+ of the content could and should be cached as Static content.

The difference between static and dynamic caching is that the static is cached only when the data changes so for example the menu of a web site is can be cached as Static information.  Even if you need to cache out many versions of the static content.

So that covers Static and Dynamic but what other types of caching is there?  Data caching when you cache the some data normally the results of say a recordset or it could be as simple as the result of a  complex function.

So why would you choose between static and dynamic caching surely they are the same thing?  well no with Dynamic caching you are interested in the age of the cache.  So you have to stat the file to see the age of the file before comparing it to the current timestamp.  This is more expensive than the Static Cache which doesn't care about the age of a file it just loads it every single time some other process will recache the files when they are required to be cached.

By using a mix of these three types of caching you can make pages that load fast and efficiently.  All content on a page can be considered a rectangle.  You the developer decide if the content in that rectangle is something that changes often (Dynamic & Static) or something that changes infrequently (Static)  but what about Data caching?

Lets say that your webpage is displaying a list of news articles well the ID's of the latest 10 articles could be cached using the Data caching so that when you are recaching the page via Dynamic caching you load the ids then loop through that small data set and load the static cached news articles.

Notice that in this example the number of database calls for this page has reduced to zero.

Let me introduce you to the PHP Programmers project Edify

In this project you will find a section for Caching with a factory class that take a driver for the type of caching that you want to do.

There are three types of caching drivers Dynamic, Statics & Requires.  As you can probably tell the requires is the data caching this is because we cache the data inside PHP tags therefore to load it the fastest way is to require the file.  This means that we do not have to convert from text to data through some complex means.

No comments:

Post a Comment