Installing Memcache on Windows

Theory

Memcache is a caching system used to speed up web applications by holding small pieces of arbitrary data, it could be broadly compared to the $_SESSION variable in PHP, but memcache works across the whole application not just on a per user basis and has been successfully proven to make great gains in speed increases on web applications.

Memcache was designed with Linux in mind and not windows, so it has posed some installation issues because Windows users are not so familiar with having to compile code from source as memcache does not come with any installation software.

Installation

  • Download memcached. Use the win32 binary version.
  • Unzip and copy the binaries to your desired directory, eg. C:\memcached. There is usually only one exe file.
  • If you are using Vista
    • right click on memcached.exe
    • click Properties
    • click the Compatibility tab
    • Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
    • Apply the changes
  • If you are using Windows 64bit then this dll file will work instead of the links mentioned above.
  • Make sure that in perInstance.php memcache server’s host has the correct value, that is either ’127.0.0.1′ or ‘localhost’ or appropriate value as per your memcached server setup.
  • Install the service using the command:
    C:\memcached\memcached.exe -d install

    from the command line

  • Start the server from the Microsoft Management Console or by running one of the following commands:
    C:\memcached\memcached.exe -d start

    or

    net start "memcached Server"

.

Installing PHP Extension

    • Check your php extensions directory for php_memcache.dll
    • If you don’t have it, Download and copy it to your php extension folder.
    • Edit your php.ini file and add following to the extensions list:
      extension=php_memcache.dll
    • Restart webserver for the new php.ini to take effect.

Testing Memcache and PHP’s Extension

Here’s some sample code from www.php.net/memcache

<?php

  $memcache = new Memcache;
  $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"
  echo "Server's version: " . $memcache->getVersion() . "<br />\n";
  $tmp_object = new stdClass;
  $tmp_object->str_attr = "test";
  $tmp_object->int_attr = 123;
  $memcache->set("key",$tmp_object,false,10);
  echo "Store data in the cache (data will expire in 10 seconds)<br />\n";
  echo "Data from the cache:<br />\n";
  var_dump($memcache->get("key"));

?>

If you have any errors, it will most likely be because the php extension didn’t run Check the webserver error log to see if it had problems starting. The most common problem is the version of memcache.dll is not compatible with the version of php you are running.

Leave a Comment

  • Faizan

    Great (Y)!!!