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

January 11th, 2012 No comments

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.randomtools.net/
*/
class Memcache {
	private $cache_dir = 'C:/path/to/directory/';
 
	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();
}
?>

Bookmark this page
[del.icio.us] [Digg] [Facebook] [Google] [Reddit] [Slashdot] [StumbleUpon]
Categories: Code, How To's, PHP Tags:

How to Install PHP APC on Fedora & CentOS

December 9th, 2011 No comments

APC (or Alternate PHP Cache) is a popular PHP PECL extension that can be used for PHP op-code caching. It is very stable and can significantly reduce page rendering times. It works by caching the PHP binaries, so that PHP doesn’t have to compile them on every request.

Installation is simple:

1. Use yum to install necessary packages.

# yum install php-pear php-devel httpd-devel pcre-devel

2. Use pecl to install the PECL apc package.

# pecl install apc

3. Create a config file that will be included in the PHP ini settings, which will load the APC binary. The default settings should be sufficient for most.

# echo "extension=apc.so" > /etc/php.d/apc.ini

4. Restart Apache:

# service httpd restart

5. Make sure it works by creating a phpinfo.php with the following code and then accessing it in your browser. If it works correctly, there will be a new APC section.

<?php phpinfo(); ?>

Bookmark this page
[del.icio.us] [Digg] [Facebook] [Google] [Reddit] [Slashdot] [StumbleUpon]
Categories: How To's, Linux, PHP Tags:

How to insert a tab character into Putty for Linux

October 20th, 2011 No comments

If you’re like me, you’ve noticed that if you’re trying to grep for a tab character, the following won’t work:

grep "\t" file.txt

The solution is to use a literal tab character, but it’s not quite so simple. You can’t insert a tab character just by hitting the tab button or by pasting one in. You need to hit Ctrl+V first:

So to type a tab in the putty console:

  • Ctrl+V
  • [TAB] character

Bookmark this page
[del.icio.us] [Digg] [Facebook] [Google] [Reddit] [Slashdot] [StumbleUpon]
Categories: How To's, Linux Tags: