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

<ol>
<li>Save the cache to a file</li>
<li>Save the cache to a large DB field</li>
</ol>

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

[{'answer_id': 4064, 'author': 'Seb', 'author_id': 189, 'author_profile': 'https://Stackoverflow.com/users/189', 'pm_score': 1, 'selected': False, 'text': '<p>You can also cache in memory which is much more efficient. Try <a href="http://www.danga.com/memcached/" rel="nofollow noreferrer">memcached</a>.</p>\n'}, {'answer_id': 4222, 'author': 'ggasp', 'author_id': 527, 'author_profile': 'https://Stackoverflow.com/users/527', 'pm_score': 3, 'selected': True, 'text': '<p>Maybe you want to explicit more precisely what you want to cache. You have all this opportunities to cache: </p>\n\n<ul>\n<li><b>Accessing the Data Base</b> 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 <a href="http://adodb.sourceforge.net/" rel="nofollow noreferrer">AdoDB</a> for example.)</li>\n<li><b>Extracting calculations from loops in the code</b> so you don\'t compute the same value multiple times. <i>Here your third way: storing results in the session for the user.</i></li>\n<li><b>Precompiling the PHP code</b> with an extension like <a href="http://pecl.php.net/package/APC" rel="nofollow noreferrer">APC Cache</a>. This way you don\'t have to compile the same PHP code for every request.</li>\n<li><b>The page sent to the user</b> 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 <a href="http://www.squid-cache.org/" rel="nofollow noreferrer">Squid</a>.</li>\n<li><b>Prefetching</b> 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. </li>\n</ul>\n\n<p>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 <a href="http://xdebug.org/" rel="nofollow noreferrer">XDebug</a>. </p>\n\n<p>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"</p>\n\n<p>Use a simple tool like <a href="http://developer.yahoo.com/yslow/" rel="nofollow noreferrer">YSlow</a> to hint other simple things to improve. Check your code again looking for opportunities to put logic in the browser (via JavaScript)</p>\n'}, {'answer_id': 4226, 'author': 'Kevin Sheffield', 'author_id': 590, 'author_profile': 'https://Stackoverflow.com/users/590', 'pm_score': 0, 'selected': False, 'text': '<p>Seconding memcached, does the simple stuff well and can go distributive and all that jazz if you need it too</p>\n'}, {'answer_id': 67170, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 0, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>RewriteEngine on\nRewriteCond %{QUERY_STRING} ^$ # let\'s not cache urls with queries\nRewriteCond %{REQUEST_METHOD} ^GET$ # or POST/PUT/DELETE requests\nRewriteCond static_cache/%{REQUEST_URI} -s # Check that this file exists and is &gt; 0 bytes\nRewriteRule (^.*$) static_cache$1 [L] # If all the conditions are met, we rewrite this request to hit the static cache instead\n</code></pre>\n\n<p>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):</p>\n\n<pre><code>php_value auto_prepend_file "pre_cache.php"\nphp_value auto_append_file "post_cache.php"\n</code></pre>\n\n<p>Then you\'d do something like this:</p>\n\n<p>pre_cache.php:</p>\n\n<pre><code>ob_start();\n</code></pre>\n\n<p>post_cache.php:</p>\n\n<pre><code>$result = ob_get_flush();\nif(!$_SERVER[\'QUERY_STRING\']) { # Again, we\'re not caching query string requests\n  file_put_contents("static_cache/" + __FILE__, $result);\n}\n</code></pre>\n\n<p>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 :)</p>\n'}]
['https://Stackoverflow.com/questions/4062', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']