Currently I know of only two ways to cache data (I use PHP but I assume that the same will apply to most languages).
- Save the cache to a file
- Save the cache to a large DB field
Are there any other (perhaps better) ways of caching or is it really just this simple?
Maybe you want to explicit more precisely what you want to cache. You have all this opportunities to cache:
From my experience, I'd bet you that your code can be improved a lot before we start to talk about caching things. Consider, for example, how well structured is the navigation of your site and how well you control the user experience. Then check your code with a tool like XDebug.
Verify also how well are you making your SQL queries and how well are you indexing your tables. Then check your code again to look for opportunities to apply the rule "read many times but write just once"
Use a simple tool like YSlow to hint other simple things to improve. Check your code again looking for opportunities to put logic in the browser (via JavaScript)
You can also cache in memory which is much more efficient. Try memcached.
Seconding memcached, does the simple stuff well and can go distributive and all that jazz if you need it too
If you're using Apache, you can use mod_rewrite to statically cache your web pages. Lets say you're using PHP, and you have a request for "/somepage.php". In your .htaccess file you put the following:
If your cache turns up empty, the request is handled by your php script as usual, so now it's simply a matter of making your php script store the resulting html in the cache. The simplest way to do this is using another htaccess rule to prepend end append a couple of php files to all your php requests (this might or might not be a good idea, depending on your application):
Then you'd do something like this:
pre_cache.php:
post_cache.php:
With some additional regular expressions in the .htaccess file we could probably start caching query string requests as well, but I'll leave that as an exercise for the reader :)