<p>What is the best way of implementing a cache for a PHP site? Obviously, there are some things that shouldn't be cached (for example search queries), but I want to find a good solution that will make sure that I avoid the 'digg effect'.</p>

<p>I know there is WP-Cache for WordPress, but I'm writing a custom solution that isn't built on WP. I'm interested in either writing my own cache (if it's simple enough), or you could point me to a nice, light framework. I don't know much Apache though, so if it was a PHP framework then it would be a better fit.</p>

<p>Thanks.</p>

[{'answer_id': 55240, 'author': 'argv0', 'author_id': 5595, 'author_profile': 'https://Stackoverflow.com/users/5595', 'pm_score': 3, 'selected': False, 'text': '<p>The best way to go is to use a proxy cache (Squid, Varnish) and serve appropriate Cache-Control/Expires headers, along with ETags : see <a href="http://www.mnot.net/cache_docs/" rel="noreferrer">Mark Nottingham\'s Caching Tutorial</a> for a full description of how caches work and how you can get the most performance out of a caching proxy.</p>\n\n<p>Also check out <a href="http://www.danga.com/memcached/" rel="noreferrer">memcached</a>, and try to cache your database queries (or better yet, pre-rendered page fragments) in there.</p>\n'}, {'answer_id': 55251, 'author': 'Pedro', 'author_id': 5488, 'author_profile': 'https://Stackoverflow.com/users/5488', 'pm_score': 4, 'selected': False, 'text': '<p>You can use <a href="http://uk3.php.net/manual/en/function.ob-get-contents.php" rel="noreferrer">output buffering</a> to selectively save parts of your output (those you want to cache) and display them to the next user if it hasn\'t been long enough. This way you\'re still rendering other parts of the page on-the-fly (e.g., customizable boxes, personal information).</p>\n'}, {'answer_id': 55370, 'author': 'TiTi', 'author_id': 435, 'author_profile': 'https://Stackoverflow.com/users/435', 'pm_score': 1, 'selected': False, 'text': "<p>You seems to be looking for a PHP cache framework.\nI recommend you the template system TinyButStrong that comes with a very good CacheSystem plugin.\nIt's simple, light, customizable (you can cache whatever part of the html file you want), very powerful ^^</p>\n"}, {'answer_id': 55495, 'author': 'georgebrock', 'author_id': 5168, 'author_profile': 'https://Stackoverflow.com/users/5168', 'pm_score': 2, 'selected': False, 'text': '<p>The PHP Smarty template engine (<a href="http://www.smarty.net" rel="nofollow noreferrer">http://www.smarty.net</a>) includes a fairly advanced caching system.</p>\n\n<p>You can find details in the caching section of the Smarty manual: <a href="http://www.smarty.net/manual/en/caching.php" rel="nofollow noreferrer">http://www.smarty.net/manual/en/caching.php</a></p>\n'}, {'answer_id': 58363, 'author': 'Ryan Doherty', 'author_id': 956, 'author_profile': 'https://Stackoverflow.com/users/956', 'pm_score': 2, 'selected': False, 'text': '<p>I would recommend <a href="http://www.danga.com/memcached/" rel="nofollow noreferrer">Memcached</a> or <a href="http://us3.php.net/apc" rel="nofollow noreferrer">APC</a>. Both are in-memory caching solutions with dead-simple APIs and lots of libraries.</p>\n\n<p>The trouble with those 2 is you need to install them on your web server or another server if it\'s Memcached.</p>\n\n<h2>APC</h2>\n\nPros:\n\n<ul>\n<li>Simple</li>\n<li>Fast</li>\n<li>Speeds up PHP execution also</li>\n</ul>\n\nCons\n\n<ul>\n<li>Doesn\'t work for distributed systems, each machine stores its cache locally</li>\n</ul>\n\n<h2>Memcached</h2>\n\nPros:\n\n<ul>\n<li>Fast(ish)</li>\n<li>Can be installed on a separate server for all web servers to use</li>\n<li>Highly tested, developed at LiveJournal</li>\n<li><p>Used by all the big guys (Facebook, Yahoo, Mozilla)</p>\n\nCons:</li>\n<li><p>Slower than APC</p></li>\n<li>Possible network latency</li>\n<li>Slightly more configuration</li>\n</ul>\n\n<p>I wouldn\'t recommend writing your own, there are plenty out there. You could go with a disk-based cache if you can\'t install software on your webserver, but there are possible race issues to deal with. One request could be writing to the file while another is reading. </p>\n\n<p>You actually could cache search queries, even for a few seconds to a minute. Unless your db is being updated more than a few times a second, some delay would be ok.</p>\n'}, {'answer_id': 60203, 'author': 'Alister Bulman', 'author_id': 6216, 'author_profile': 'https://Stackoverflow.com/users/6216', 'pm_score': 1, 'selected': False, 'text': "<p>Simple caching of pages, or parts of pages - the Pear::CacheLite class.  I also use APC and memcache for different things, but the other answers I've seen so far are more for more complete, and complex systems.  If you just need to save some effort rebuilding a part of a page - Cache_lite with a file-backed store is entirely sufficient, and very simple to implement.</p>\n"}, {'answer_id': 60242, 'author': 'chrisntr', 'author_id': 4455, 'author_profile': 'https://Stackoverflow.com/users/4455', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://whatcd.github.io/Gazelle/" rel="nofollow noreferrer">Project Gazelle</a> (an open source torrent site) provides a step by step guide on setting up Memcached on the site which you can easily use on any other website you might want to set up which will handle a lot of traffic.</p>\n\n<p>Grab down the source and read the documentation.</p>\n'}, {'answer_id': 63451, 'author': 'Steve Clay', 'author_id': 3779, 'author_profile': 'https://Stackoverflow.com/users/3779', 'pm_score': 4, 'selected': True, 'text': '<p>If a proxy cache is out of the question, and you\'re serving complete HTML files, you\'ll get the best performance by bypassing PHP altogether. Study how <a href="http://ocaoimh.ie/wp-super-cache/" rel="noreferrer">WP Super Cache</a> works. </p>\n\n<p>Uncached pages are copied to a cache folder with similar URL structure as your site. On later requests, mod_rewrite notes the existence of the cached file and serves it instead. other RewriteCond directives are used to make sure commenters/logged in users see live PHP requests, but the majority of visitors will be served by Apache directly.</p>\n'}]
['https://Stackoverflow.com/questions/55223', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3654/']