Monday, November 28, 2011

HOW TO: Install Memcache on XAMPP (Windows 7)

Hey there, its me (Stan nesi) again !!!LOL

Memcached solves this problem for you by letting you write frequently accessed data directly to RAM, where it can be fetched almost instantaneously; plus you can tell memcached how long to store the cached data and it will automatically purge stale data without any further intervention from you, the programmer.

Many web sites & applications such as Facebook, LiveJournal, Flickr, Slashdot, WikiPedia/MediaWiki, SourceForge, Digg and Twitter use memcached to enhance their performance.

I'm often asked "How can I run Memcache on my localhost server?" "Is Memcache a php extension or a serve?" after this HOW TO: all your questions will be answered and I choose to use XAMPP because most people I know uses XAMPP, I recently figured it out recently anyway...because i mostly use File Cache but I use Memcahe now! :D So Lets begin:


1.   Edit your XAMPP php.ini (becareful)
Open up the php.ini file located at "C:/xampp/php/php.ini." find this line:
extension=php_memcache.dll
and replace it with
extension=php_memcache.dll
If you cannot find this line append the following line of codes after all of the other extension declarations:
extension=php_memcache.dll

[Memcache]
memcache.allow_failover = 1  
memcache.max_failover_attempts=20  
memcache.chunk_size =8192  
memcache.default_port = 11211


Make sure there are no semicolon (;) before the extension.

2.   Download the necessary php extension (.dll) "php_memcache.dll"
Believe it or not this step was quite troublesome for me myself. All resources I search online all directed to this download location: http://downloads.php.net/pierre/. to download the php_memache.dll extension file. Unfortunately I found only VC9-x86 complied version of php_memcache.dll on that site, check it out for yourself. But  I'm  using XAMPP 1.7.4 and i'm quite sure you use same which requires VC6-x86 complied version of php_memcache.dll. incase you might me wondering how would you know what complier (VC9 or VC6) and architecture (x86) is used, all information are located in your phpinfo(); see below image with markings.
YAY!!! douring my search I was able to some sources where you can download the VC6-x86 dll version.
I think that's enough. After successfully downloading the file, Unzip and copy the file php_memcache.dll and put it into your php extension folder. located at C:/xampp/php/ext/

3.   Download and installing Memcache server for windows
Download the Memcache.exe from http://code.jellycan.com/memcached/ and make sure it a win-32 binary or use this for direct download http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip

After completion of download, unzip and put the memcache.exe file into any desired directory of your choice (e.g. c:/memcached/) I know you wanna know how mine was...LOL!! i used "C:/servers/memcached/". I'm serious i'm not lying... ok here's my proof!!:P

Installing the Memcache Service
Open command line in Windows Administrative Mode.
Click -> Start, in the search bar laballed "Search programs and files" type "cmd.exe" and wait for programme to appear, then right-click on the icon and click "run as administrator".

Type the following into the command line using the right directory (e.g. c:/memcached/)
> c:/memcached/memcached.exe -d install
If you don't get any error it means the memcached service installed and it's working

Starting Memcached Server
There are different ways to run the Memcached service
  • By typing the any of the following into the command line 
     > c:/memcached/memcached.exe -d start or
     > net start "memcached server"
  • By double-clicking on the memcache.exe Icon located in "c:/memcached/mecached.exe"
  • By running memcache from services by finding "memcached server" and clicking on the start button



Restart XAMPP Apache
Restart XAMPP Apache for all changes to take effect/

Testing Memcache
create a new .php file and paste the following line of codes
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$ver = $memcache->getVersion();
echo "Server's version: ". $ver ."<br/>\n";
$oTmp = new stdClass;
$oTmp->str_attr = 'testing memcached';
$oTmp->int_attr = 12345;
$memcache->set('key', $oTmp, false, 10) or
die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
<br/>\n";
$res = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($res);
?>
...and run on your Localhost!!! VOILA!!!

Just in case it doesn't work: I ran the c:/memcached/memcached.exe file as administrator. This opens the ports on the Windows firewall which might solve some problems.

There will me more!!!LOL Feel free to contribute by dropping your comments: