How to install and use memcached in Windows for PHP?

后端 未结 4 642
执念已碎
执念已碎 2020-12-15 00:01

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 t

相关标签:
4条回答
  • 2020-12-15 00:08

    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

    0 讨论(0)
  • 2020-12-15 00:21

    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/

    0 讨论(0)
  • 2020-12-15 00:27

    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
    0 讨论(0)
  • 2020-12-15 00:27

    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

    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;
    }
    
    0 讨论(0)
提交回复
热议问题