Implementing Caching

I am looking for a decent caching solution for my php applications. The thing is we use shared hosting at the moment, so installing external modules or plugins are quite a mess.

we will however be changing to a dedicated server on which I would like to run a memcache installation, thus I need a option that allows seemless changing to memcache.

I can offcourse write something from scratch, but would rather spend that time improving on an already developed solutions.

i am open for discussions, so let the comments roll. :rofl:

Usually yes, this is the case.

I’m not going to elaborate on what I plan because I’m not sure it will work. If it does though I’ll be sharing it. Conceptually though I think it has a great shot.

Caching complete pages, it seems to me, is a waste of time.
There are a multitude of methods to do this (squid, varnish, nginx to name a few) without needing to write a single line.

I think key to using them is learning how to purge stale pages from the cache, once something changes.

Next would be fragment caching, where a fragment is a piece of HTML, and associated cache headers (Expires, Content-Control etc) which are combined into a final page.

Things like ESI which Varnish as partial support, would enable different caching policies for static and dynamic sections of pages.

Also, would you go for memcache if you had the choice, or would you stick to caching to the file system?

Or maybe a combination of the both memcache and file system? I prefer the later.

:smiley: (We shall see.)

I will definately check it out. Thank you very much.

While that’s true it’s very limiting. It prevents anything dynamic happening on the page (e.g. greeting the logged in user, displaying a shopping basket, etc). In the real world it’s almost always better to just cache parts of the output.

The most powerful cache possible is a static file the webserver reads without starting PHP at all.

I use Xcache (http://xcache.lighttpd.net) on my sites - it works nice in fastcgi env (sharing cache between workers) and you can stuff data into ram for later use. You can write a brain-dead wrapper class for its functions.
APC is very similar, although APC used to crash for me a lot in the past (under apache mod-php) and so I decided to drop it. And I think it still does not support fastcgi, so it’s not an option for me anymore.

I also think about moving to memcache (along with session data) - gonna test it soon. But I think it won’t make much difference (memcache or xcache), when you use just 1 machine.

Aye that is a good point, when possible let the web server or other devices further down the chain handle most of the caching. For IIS 7 you can use Output Caching that is built in. I’m still investigating solutions for Apache.

Zend’s caching is awesome, but I am unable to make use of it as it will conflict with our naming convention and thus make autoloading of classes difficult.

Regarding page caching, I do agree, allow the web server to handling caching of complete pages. Let your code cache results and database query results.

Keep in mind you can write your own basic filesystem cache class in about 10 lines:


class Cache {
	public $tmpDir = '/tmp/';

	public function isCached($id) {
		return file_exists($this->tmpDir . $id);
	}

	public function save($id, $data) {
		return file_put_contents($this->tmpDir . $id, serialize($data));
	}

	public function load($idd) {
		return $this->isCached($id) ? unserialize(file_get_contents($this->tmpDir . $id)) : null;
	}
}

Add GC or a method to reload it and you’re pretty much good to go…

Caching solution, singular? If you’re going to be caching, do it at as many levels as works best; there are a wide range of caching strategies available at many points between the visitor’s browser and the deepest, darkest parts of your application.

http://framework.zend.com/manual/en/zend.cache.introduction.html

Can later swap out the means of caching, file or memcache, without changing your caching logic.

Also for MySQL data caching, http://blog.ulf-wendel.de/?p=295

@dan7 APC supports fastcgi, we’ve been using in with Lighttpd+FastCGI for at least two years now. It’s also much more stable nowadays, and will become an integral in PHP in one of the future PHP versions. Or so they say. :slight_smile:

Also, the purpose of APC and XCache differ from that of Memcache. They’re primarily opcode cachers that run on a PHP node, and aren’t sharing their cache, while Memcache is a general-purpose cache, is accessible via the network and can share its data to several servers. It also doesn’t do any opcode caching, and is somewhat slower, but has more memory available in a typical install.

They are not interchangeable, but they work fine together, though. As I mentioned eariler, we’re using LayerCache for layered caching of data from DB -> Memcache -> APC (could also be XCache) -> in-request local PHP array cache, where each layer is faster, but also has less memory available.

I’m developing a layered cache framework, that enables you to easily cache items in any number of layers with different TTLs. It also supports prefetch (fetching items that haven’t expired yet, to avoid database slams upon expiration).

We’re using it in production for over a year now, usually with DB as the source, then a Memcache layer, then APC, and sometimes Local (basically just a PHP associative array, to avoid hitting even APC, if the same object was already retrieved during the same request). Handles lots of requests and works great.

If you’re interested, check out LayerCache (basic documentation [URL=“http://code.google.com/p/layercache/w/list”]here).

Stumbled appon Cache_Lite. This is available as a pear package or a download. Seems to have functionality to cache to memory and file system. Does any one have experience with this package?

what exactly do you want to cache?

The whole page output? Output of specific views? Return values of specific resource heavy methods?

Mostly results from a database. And return types of resource intense functions.