tags:

views:

1504

answers:

8

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'.

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.

Thanks.

+4  A: 

The best way to go is to use a proxy cache (Squid, Varnish) and serve appropriate Cache-Control/Expires headers, along with ETags : see Mark Nottingham's Caching Tutorial for a full description of how caches work and how you can get the most performance out of a caching proxy.

Also check out memcached, and try to cache your database queries (or better yet, pre-rendered page fragments) in there.

argv0
+7  A: 

You can use output buffering 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).

Pedro
Not sure why you got downmodded - that's an entirely possible way of doing it.
ceejayoz
Thank you. Yes, I don't understand it myself either. It's quite useful in several situations where other pre-fab caching solutions don't apply.
Pedro
+1  A: 

You seems to be looking for a PHP cache framework. I recommend you the template system TinyButStrong that comes with a very good CacheSystem plugin. It's simple, light, customizable (you can cache whatever part of the html file you want), very powerful ^^

TiTi
+2  A: 

The PHP Smarty template engine (http://www.smarty.net) includes a fairly advanced caching system.

You can find details in the caching section of the Smarty manual: http://www.smarty.net/manual/en/caching.php

georgebrock
+2  A: 

I would recommend Memcached or APC. Both are in-memory caching solutions with dead-simple APIs and lots of libraries.

The trouble with those 2 is you need to install them on your web server or another server if it's Memcached.

APC

Pros:
  • Simple
  • Fast
  • Speeds up PHP execution also
Cons
  • Doesn't work for distributed systems, each machine stores its cache locally

Memcached

Pros:
  • Fast(ish)
  • Can be installed on a separate server for all web servers to use
  • Highly tested, developed at LiveJournal
  • Used by all the big guys (Facebook, Yahoo, Mozilla)

    Cons:
  • Slower than APC

  • Possible network latency
  • Slightly more configuration

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.

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.

Ryan Doherty
A: 

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.

Alister Bulman
A: 

Project Gazelle (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.

Grab down the source and read the documentation.

chrisntr
+2  A: 

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 WP Super Cache works.

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.

mrclay
Thanks, that's the answer I was looking for. :)
different