How to install PHP Memcache on Windows WAMP, Linux for any operating system (without really installing)

I have a site that runs on Linux in production that uses Memcache. Installing Memcache on Linux is a cinch. I was setting this site up in my development environment however (Windows 7 64-bit, with WAMP, specifically Apache 2 and PHP 5.3) and I quickly discovered it’s not so easy. For the life of me, I could not find the correct php_memcache.dll file.

After spending a ton of time unsuccessfully install Memcache on my local machine, I realized that I don’t even need the memory caching functionality of Memcache, I just need the functions to work.

After some thought, I decided to just implement the class and functions that Memcache provides. Following is what I came up with. It just uses a specified directory to store cache in serialized files on the hard drive. Obviously it doesn’t exactly simulate real production Memcache, but it allows you to work from any box without having to install it.

Here’s what I came up with:

<?php
/*
Fake PHP Memcache class
Use Memcache functions without installing. Useful in development environments.
 
Dan Barnett
http://www.danbarnett.net/
*/
class Memcache {
	private $cache_dir = FAKE_MEMCACHE_DIR;
 
	public function close() {}
 
	public function connect() {
		return true;
	}
 
	public function pconnect() {
		return $this->connect();
	}
 
	public function getVersion() {
		return -1;
	}
 
	public function get($key) {
		return unserialize(file_get_contents($this->cache_dir . md5($key) . ".txt"));
	}
 
	public function set($key, $value) {
		return file_put_contents($this->cache_dir . md5($key) . ".txt", serialize($value));
	}
 
	public function flush() {
		foreach (glob($this->cache_dir . '*.txt') as $file) {
			unlink($file);
		}
		return true;
	}
}
 
function memcache_close() {}
 
function memcache_connect() {
	global $memcache;
	$memcache = new Memcache();
}
 
function memcache_pconnect() {
	return memcache_connect();
}
 
function memcache_set($key, $value) {
	global $memcache;
	return $memcache->set($key, $value);
}
 
function memcache_get($key) {
	global $memcache;
	return $memcache->get($key);
}
 
function memcache_get_version() {
	global $memcache;
	return $memcache->getVersion();
}
 
function memcache_flush() {
	global $memcache;
	return $memcache->flush();
}
?>

[ad]

One thought on “How to install PHP Memcache on Windows WAMP, Linux for any operating system (without really installing)”

Leave a Reply

Your email address will not be published. Required fields are marked *