views:

227

answers:

4

Currently I know of only two ways to cache data (I use PHP but I assume that the same will apply to most languages).

  1. Save the cache to a file
  2. Save the cache to a large DB field

Are there any other (perhaps better) ways of caching or is it really just this simple?

+1  A: 

You can also cache in memory which is much more efficient. Try memcached.

Sebastjan Trepča
+1  A: 

Maybe you want to explicit more precisely what you want to cache. You have all this opportunities to cache:

  • Accessing the Data Base where you cache the data first correctly tuning your RDBMS, then using a layer to delegate the decision to detect multiple queries for the same data (with AdoDB for example.)
  • Extracting calculations from loops in the code so you don't compute the same value multiple times. Here your third way: storing results in the session for the user.
  • Precompiling the PHP code with an extension like APC Cache. This way you don't have to compile the same PHP code for every request.
  • The page sent to the user making sure you're setting the right META tags (do a good thing for the world and don't use ETL at least absolutly necessary); or maybe making dynamic pages completely static (having a batch process that generates .html pages); or by using a proxy cache like Squid.
  • Prefetching and by this I refer all those opportunities you have to improve the user experience just by doing things while the user don't look your way. For example, preloading IMG tags in the HTML file, tunning the RDBMS for prefectching, precomputing results storing complex computations in the database, etc.

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)

ggasp
A: 

Seconding memcached, does the simple stuff well and can go distributive and all that jazz if you need it too

Kevin Sheffield
A: 

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:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$ # let's not cache urls with queries
RewriteCond %{REQUEST_METHOD} ^GET$ # or POST/PUT/DELETE requests
RewriteCond static_cache/%{REQUEST_URI} -s # Check that this file exists and is > 0 bytes
RewriteRule (^.*$) static_cache$1 [L] # If all the conditions are met, we rewrite this request to hit the static cache instead

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):

php_value auto_prepend_file "pre_cache.php"
php_value auto_append_file "post_cache.php"

Then you'd do something like this:

pre_cache.php:

ob_start();

post_cache.php:

$result = ob_get_flush();
if(!$_SERVER['QUERY_STRING']) { # Again, we're not caching query string requests
  file_put_contents("static_cache/" + __FILE__, $result);
}

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 :)

Erlend Halvorsen