Koray Tugay
Koray Tugay

Reputation: 23800

How to install and use memcached in Windows for PHP?

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?

Upvotes: 10

Views: 50003

Answers (5)

b126
b126

Reputation: 1254

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

Upvotes: 0

Jonathan
Jonathan

Reputation: 7098

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;
}

Upvotes: 1

DroidOS
DroidOS

Reputation: 8890

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

Upvotes: 15

Venu
Venu

Reputation: 7279

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/

Upvotes: 0

Michael Marr
Michael Marr

Reputation: 2099

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

Upvotes: 3

Related Questions