10

I have installed memcached binary file in Windows 7 and started it as server.

When I type wmic process get description, exetuablepath | findstr memcached.exe I get the response: memcached.exe c:\memcached\memcached.exe on command line.

When I try running the sample code on php.net, I get on my browser:

Fatal error: Class 'Memcache' not found in C:\DocumentRoot\Framework\index.php on line 3 Call Stack: 0.0010 335928 1. {main}() C:\DocumentRoot\Framework\index.php:0

So, what is it that I am doing wrong? I am using memcache.dll since memcached.dll does not exist for Windows I believe?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

5 Answers5

15

A note to anyone who runs into issues with getting memcached working on Windows.

  • For starters ensure that you have the right version of the memcached dll and that it is accessible. There is a wide selection available at http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/ and it is all too easy to choose the wrong version of memcached!.
  • If you are running PHP 5.5 you will additionally require php5.dll. You can get this here
  • You may need to edit your environment PATH settings so this dll can be found. Go to My Computer->Properties->advanced and click on Environment Variables to view/edit the path. You need to restart the computer if you edit this.
  • Ensure that the memcached server is installed. Ctrl + Alt + Del and check that memcached is present in your list of services
  • If not you need to *install it from the Cmd prompt run as administrator (from the start menu, choose accessories, click on command prompt and choose to run as administrator) c:\pathtomemcached\memcached.exe -d install
  • follow this with c:\pathtomemcached\memcached.exe -d start or net start “memcached Server”. On my installation the former does not work
  • Likewise I am unable to start memcached from the Services tab of the Task Manager
  • It is handy to be able to play around with memcached at a low level so enable telnet, if required, and from the command prompt type telnet. Now open port 11211 and try using memcached
  • It is also useful to be able to keep tabs on what is happening in memcached. phpMemCacheAdmin is by far the best tool for the job
DroidOS
  • 8,530
  • 16
  • 99
  • 171
  • I think that this: **"it is all too easy to choose the wrong one"** can be bolded in this answer. I've lost ALL day because of choosing wrong version. I want also to add that it is VERY important to have matching **86x/64x** WAMP and memcache.dll version AND WAMP compiled VC6/VC9 and matching **VC6/VC9** version of memcache.dll. I have WAMP VC9 and I was using memcache.dll VC6, that's why it wasn't work. – Rob May 01 '14 at 20:12
  • @Rob - Good point! I have edited the answer and highlighted the important bits of the answer. – DroidOS May 02 '14 at 06:42
  • For determining whether to use Thread Safe or Non Thread Safe, this helped me: http://stackoverflow.com/a/5800346/470749 – Ryan Apr 04 '17 at 00:10
  • 2
    I'm super confused about this. Memcached (with a 'd') and Memcache are not the same thing. Every other post I've read online over the past few hours suggests that Memcached (ending in a 'd') cannot be installed on Windows. Are you *sure* that you got Memcached working on Win? Could it work with PHP 7.0.1 x64 thread-safe? I've been stressing for hours trying to get this to work but keep getting `PHP Warning: PHP Startup: Unable to load dynamic library 'C:/php-7.0.1-Win32-VC14-x64/ext/php_memcache.dll' - The specified module could not be found.` and/or `Class 'Memcached' not found`. – Ryan Apr 04 '17 at 00:51
  • No support for php 7.2? I see 5.5 - 5.6? – Jonathan Sep 21 '18 at 22:54
  • My 8 year old answer here still gets regular upvotes. I thought it might just be worth pointing out that in the year 2022+ for most problems Redis provides a far more flexible and powerful solution than memcached. If the choice is yours to make choose Redis over memcached. There are excellent of instructions for installing [shorturl.at/jlmwS](Windows ) & on Ubuntu 20.04 [shorturl.at/hpvV9](Ubuntu 20.04) – DroidOS Jan 13 '22 at 06:04
3

Based on the comments, I assume you have not downloaded and installed memcached, but have successfully installed the memcached module for PHP. Basically, you've gotten the car keys, but don't have the car.

memcached is built for Linux, but it has been ported by others to Windows. This tutorial is old, but it might be what you're looking for: http://www.codeforest.net/how-to-install-memcached-on-windows-machine

Michael Marr
  • 2,101
  • 14
  • 13
2

This is for future vistors!

  1. check phpinfo() and see if it's listed.
  2. If not, check whether extension is enabled in php.ini and then check apache error logs for error message! dll should be complied with the same compiler the php is. (VC9 or VC6) btw, memcache.dll is fine

You can get the php extension "memcache" to use memcached with php on windows here http://downloads.php.net/pierre/

Memcached is the server daemon and you can get it for windows here http://splinedancer.com/memcached-win32/

Venu
  • 7,243
  • 4
  • 39
  • 54
1

Your composer.json should have ext-memcached listed in it but it won't install, it'll just throw an error if it's missing. Here's various ways to get it:

Windows Binary Route

AFAIK as of 2018 there is no binary Windows port of JUST Memcached for PHP 7 But there is a pre-packaged version in Laragon or alternatively Winginx enter image description here

Windows DLL Route

There's a handful of people offering compiled DLLs on github (64-bit, and thread-safe offered)

Windows Subsystem For Linux Route

ubuntu
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt install php-memcached

Restart php fpm if using it sudo service php7.2-fpm restart

Compile from Source Route

You can compile the php bindings but the windows package of memcached has been broken for 4 years (as of 2018)

Local-Only Cache Files Polyfill Route

Here's a dirty wrapper around Memcached called StaticCache you can use in a pinch to read/write values from disk. It's obviously way slower than memcached so it's only meant to be a shoe-in for Windows development. If you get fancy, you could define this as a polyfill by the same name

function StaticCacheClear()
{
    foreach (scandir(sys_get_temp_dir()) as $file) {
        if (StringBeginsWith($file, "staticcache"))
        {
            $path = sys_get_temp_dir() ."/". $file;
            unlink($path);
        }
    }
    global $Memcache;
    if ($Memcache) $Memcache->flush();
}

// REMOVE if you don't want a global way to clear cache
if (isset($_GET['clear_static_cache'])) {
    StaticCacheClear();
}

function MemcacheGet($key)
{
    global $Memcache;
    $value = $Memcache ? $Memcache->get($key) : (file_exists($key)?file_get_contents($key):null);

    return !$Memcache? $value : (Memcached::RES_NOTFOUND === $Memcache->getResultCode() ? null : $value);
}


function StaticCacheKey($key)
{
    global $Memcache;
    $cacheVersion = "MY_APP_VERSION_HERE";
    $uniqueKey = "staticcache_{$key}_"  . date("Ymd") . "$cacheVersion.cache";
    $filename = sanitize_file_name($uniqueKey);
    $filename = sys_get_temp_dir() . '/' . $filename;
    return $Memcache ? $uniqueKey : $filename;
}

function StaticCacheWrite($key, $value)
{
    global $Memcache;
    if (isset($_GET['disable-cache'])) return null;
    if ($Memcache)
        $Memcache->set(StaticCacheKey($key), serialize($value));
    else
        file_put_contents(StaticCacheKey($key), serialize($value));
}

function StaticCacheRead($key)
{
    global $Memcache;
    $key = StaticCacheKey($key);
    $value = MemcacheGet($key);
    return $value !== null ? unserialize($value) : null;
}
Jonathan
  • 6,741
  • 7
  • 52
  • 69
0

For future visitors, there is a native binary Windows port, running up-to-date versions for Windows 32 and 64 bits.

We are personally running v1.6.8 on both PHP7 and PHP8 Win x64 production servers. It works pretty fine and is much faster than the previous v1.4.4

Memcached for Windows: https://github.com/jefyt/memcached-windows

Another Cygwin version is also available here and runs well too https://github.com/nono303/memcached

b126
  • 544
  • 4
  • 11