Think before you turn it on
The catch 22 of caching in EE
ExpressionEngine's caching in both 1.x and 2.x are as misused as nesting exp:channel:entries tags inside each other.
First thing's first. What kind of environment are you in? Is this a high performance, load balanced setup with network-based storage and your database on its own server? Is this a cheap VPS, or perhaps a "dedicated" server that's nothing more than a 4-5 year old tower sitting on a bread rack? (ohh, but it's got RAID) :: cough ::
Okay, let's go over the assumption that we're smart enough to not go the cheap "dedicated" server route. We're on a VPS a la Linode, Slicehost or Rackspace. Starting off, we're running web and database on the same server, so CPU and memory are at a premium.
Ask yourself these questions:
- Am I having performance issues?
- What are said issues?
- What has been done to solve them?
You're doing a call to Twitter or another web service to grab data.
To answer the questions above. Yes, I'm having slow page loads. They are from grabbing data from another website on each page load. Well, I can't speed up data coming from another data center, so tag caching seems appropriate to me in this situation. Disk I/O will increase just a bit due to checking the cache. However, you're going to have better page load speeds because you aren't going over the internet on every request to get your data. Plus, the web service is going to appreciate you doing that as well.
In this hypothetical situation, let's say that we have one template where we had to break all rules of taste and the template is a mess. We knew it was going to cause performance issues, but there's no good way around it. Shit happens; we deal with it. All PHP has been moved to a plugin, PHP parsing is turned off on this template because we know the boost that'll give. Looking at the output profiler, we see a ton of database queries, like 500, even after refactoring. They are also taking a decent amount of time, and overall page load time totally sucks.
Here, ask for help. Another set of eyeballs on the template might help spot things you can clean up. Maybe consider removing elements from the page? Given the number of queries, I'd say there's probably a ton going on.
When all else fails, I think it's acceptable to plop some caching on the entire template. If you have a ton of exp tags on a page, you're going to probably get performance degradation due to the sheer number of cache files trying to be included, so template-wide caching is better than a bunch of tag caches. I recommend working like a fiend to refactor the template. The template sucks, so fix it.
A note about query caching
This should not be used unless you have done a cost / benefit analysis on actual db server load vs file system I/O.
So, the moral of the story and points to take home are:
- Never just turn caching (or anything, really), on for the sake of it. Make an informed decision as to why you should.
- Learn, study and ask questions.
- Always develop with the template profiler and output profiler in EE on.
- Always refactor crappy code to good code first.
Oh, and always run APC. That helps too. :)