Tuesday, February 5, 2013

PHP 5.4, Memcached and Windows 7


Recently I was asked to get memcached working with PHP 5.4.8 on my Windows 7 64 bit machine at work, took a while and some looking around, but I was able to get it working successfully.  Below are the steps which I took to get it to work.  Hopefully this helps someone else in the future.

Assumptions:

  1. You have apache, php 5.4 installed.  my directories are as follows:
    c:\etc\php
    c:\etc\apache
  2. You are an admin on your machine... If you aren't get access.
  3. I'm using Windows 7, 64 bit
  4. In reading this you realize that this is what has worked for ME, I can't promise that it will work for you, nor do I take any responsibility for any of the files below.  They didn't break my machine, but who knows what they will do to yours. Use at your own risk
Let's get down to business! 
  1. Download this http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
  2. Unzip here: c:/etc/memcahced
  3. Right click ->; properties ->; compatibility ->; run as admin
  4. Go to command line:
    c:\etc\memcached\memcached.exe –d install
  5. If window throws an error saying that it is missing or can not find "MSVCR71.dll" do the following, else go to #6
    1. Download this: http://www.box.net/shared/jn252o074v
    2. Copy file to c:\windows\system32
    3. Try #4, if it works, great go to #6, 
    4. Didn’t work huh? Ok, move file to c:\windows\system and try again.
    5. Still didn't work? I've heard going placing in C:\windows\sysWOW64 works... but I haven't tried that.
  6. Go to services.msc, start service (find where it says memcached, right click and hit 'start')
  7. Confirm everything is proper by going to regedit and making sure you have this:
    HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached
    to access regedit open a run window and type 'regedit'
  8. Raise memory limit (if you want):
    c:\etc\memcached\memcached.exe –d runservice –m 512
  9. Download PHP .dll (now as I've said earlier, this is only for php 5.4): http://windows.php.net/downloads/pecl/releases/memcache/3.0.6/php_memcache-3.0.6-5.4-ts-vc9-x86.zip
  10. Unzip contents to c:\etc\php\ext
  11. Add the following to php.ini
    extension=php_memcache.dll
  12. Restart server
  13. Add this to a test page:
//Create a new instance of memCached
$memcache = new Memcache;

//Of course you can use "localhost" instead of the IP if you want
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");

//Get the version, cause why not
$version = $memcache->getVersion();
echo "Memcache's version: $version
\n";

//Make some object
$tmp_object = (object) null;
$tmp_object->;someString = 'Testing...';
$tmp_object->;someInt = 9083202;

//Get the contents of the memcache
$get_result = $memcache->;get('key');
echo "-- Data currently in the cache:
\n";
var_dump($get_result);

//set the memcache
$memcache->set('key', $tmp_object, false, 15) or die ("Failed to save data at the server");

echo "
\n-- Data is stored in the cache, will expire in 15 seconds
\n";
//Get the contents of the memcache
$get_result = $memcache->;get('key');
echo "-- Data currently in the cache:
\n";
var_dump($get_result) ;


Now, assuming you have no errors, you are good to go!  Hope this helped!


5 comments:

Eli said...

Nice one dude! Works for me! Thanks.

Unknown said...

Please note that this is Memcache and not Memcached. Has someone been able to setup the php extension for Memcached?

sanju said...

Your idea is working for memcache only not for memcached.Please mention it in your post title

sanju said...
This comment has been removed by the author.
Hien said...

Thank you, it's great.
But link http://windows.php.net/downloads/pecl/releases/memcache/3.0.6/php_memcache-3.0.6-5.4-ts-vc9-x86.zip

Now it need change: http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/php_memcache-3.0.8-5.3-ts-vc9-x86.zip
Tks