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)!!!

  • Iss

    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.
    Hi, not sure what this means?

  • Stafford McKay

    Can you join the Dev Session today so we can go over your issue? We have Developer Sessions every Tuesday at 10:00AM Chicago Time (http://zurmo.org/forums/index.php?/calendar/event/2-developer-learning-session/).

  • pan sengtat

    I downloaded the DLL file from the given link and after starting Apache/MySQL through XAMPP, I got this message:

    PHP Startup: memcache: Unable to initialize module
    Module compiled with module API=20090626
    PHP compiled with module API=20100525
    These options need to match.

    Looks like the memcache.dll file in the download link is for an older PHP version, a workaround can be used based on this link for newer PHP versions: http://stackoverflow.com/questions/10347743/windows-php-memcache-dll-for-php-5-4

  • Pierre Joye

    A quick note:

    All pecl packages should be available here (snapshots or releases):

    http://windows.php.net/downloads/pecl/

    including memcache releases.

  • steven lee

    Nice tutorial,I have did as it,and it works well.

  • Mahendra Varma

    well can you guide me how i installed memcached in local server wamp. i download http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/ from here .dll file for my php version 5.4.16 and put that wampbinphpphp5.4.16ext here and after that i restart my wamp but still in php.ini i can’t show php_memcached.dll . please any one give suggestion .

    • abc

      php_memcache.dll != php_memcached.dll – php_memcacehd.dll doesn’t exist :/
      the different situation is at UNIX :) memcache.so exists and memcached.so exists:)

  • mahesh

    I installed memcached by reading your article on Windows7 but unfortunately i keep getting error as follows:—
    Fatal error: Class ‘Memcache’ not found in D:xampphtdocstestmemcachetest.php on line 2

    Line 2: $memcache = new Memcache;

    • Asif Mansoor

      Did you check the php logs in xampp to see if it is able to load the memcache extension?

  • Asif Mansoor

    Nice article. Thanks for sharing..

  • William Tsuruta

    Hi thanks for so nice article.
    Well I need your help please because Ive installed and using the page to check I can receive the version f server but nothing about the data on cache.
    This is a portion of my info.php and as I think that the memcahed was loaded in php but why I dont have nothing as the cache result in the page test?
    My php version is 5.3.26 threadsafe vc9 in a win2K3 standard edition
    the version of the memcached is the php_memcache-3.0.8-5.3-ts-vc9-x86
    Also please in this version we have the file php_memcache.pdb that like to be the database of the memcached but where it must be placed?

    memcache

    memcache supportenabled
    Version 3.0.8
    Revision $Revision: 329835 $

    DirectiveLocal ValueMaster Value
    memcache.allow_failover11
    memcache.chunk_size81928192
    memcache.compress_threshold2000020000
    memcache.default_port1121111211
    memcache.hash_functioncrc32crc32
    memcache.hash_strategystandardstandard
    memcache.lock_timeout1515
    memcache.max_failover_attempts2020
    memcache.protocolasciiascii
    memcache.redundancy11
    memcache.session_redundancy22

    Thanks for any help.

  • Michael

    Thank you for that piece.
    Could windows users still possibly get a
    recent php dll extension for the memcached package (as opposed to
    memcache)? My current use case requires the memcached extension, and
    pecl has no dll’s for that.

  • Albert H

    I downloaded the 64bits version and the cmd keep waiting in black screen, i can’t write in the console any idea?
    https://uploads.disquscdn.com/images/4560d2cfd69a8451cbcdd09dc6a7c004b893ba99d6285e027a379a7ae3349081.png