How do I implement a HTML cache for a PHP site?

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.


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


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.


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.

Tags:

Html

Php

Caching