【问题标题】:How to install and use memcached in Windows for PHP?如何在 Windows 中为 PHP 安装和使用 memcached?
【发布时间】:2025-12-27 17:00:13
【问题描述】:

我已经在 Windows 7 中安装了 memcached 二进制文件并将其作为服务器启动。

当我输入 wmic process get description, exetuablepath | findstr memcached.exe 我得到响应:memcached.exe c:\memcached\memcached.exe 在命令行上。

当我尝试在 php.net 上运行示例代码时,我进入了我的浏览器:

致命错误:在第 3 行的 C:\DocumentRoot\Framework\index.php 中找不到类“Memcache”调用堆栈:0.0010 335928 1. {main}() C:\DocumentRoot\Framework\index. php:0

那么,我做错了什么?我正在使用 memcache.dll,因为我相信 Windows 不存在 memcached.dll?

【问题讨论】:

  • 希望您已启用扩展! :)
  • 我有.. 仍然没有运气。我应该准确下载哪个 dll?
  • 你确定扩展已经加载了吗?检查phpinfo() 并查看它是否已列出。
  • 你有什么错误吗?还是 dll 显示在 phpinfo 中?
  • 检查 apache 错误日志中的错误信息! dll 应该使用与 php 相同的编译器。 (VC9 或 VC6)顺便说一句,memcache.dll 很好

标签: php memcached


【解决方案1】:

给在 Windows 上运行 memcached 时遇到问题的任何人。

  • 对于初学者,请确保您拥有正确版本的 memcached dll 并且它是可访问的。 http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/ 提供多种选择,很容易选择错误的 memcached 版本!
  • 如果您运行的是 PHP 5.5,您还需要 php5.dll。你可以得到这个here
  • 您可能需要编辑您的环境 PATH 设置,以便可以找到此 dll。转到我的电脑->属性->高级,然后单击环境变量以查看/编辑路径。如果您对此进行编辑,则需要重新启动计算机。
  • 确保已安装 memcached 服务器。 Ctrl + Alt + Del 并检查 memcached 是否存在于您的服务列表中
  • 如果不需要,您需要*从 Cmd 提示符安装它 以管理员身份运行(从开始菜单中选择附件,单击命令提示符并选择以管理员身份运行) c:\pathtomemcached\memcached.exe -d 安装
  • 使用 c:\pathtomemcached\memcached.exe -d start 或 net start “memcached Server” 进行操作。在我的安装中,前者不起作用
  • 同样,我无法从任务管理器的“服务”选项卡启动 memcached
  • 如果需要,可以很方便地在低级别使用 memcached,例如enable telnet,并从命令提示符键入 telnet。现在打开端口 11211 并尝试使用 memcached
  • 能够密切关注 memcached 中发生的事情也很有用。 phpMemCacheAdmin 是迄今为止最好的工作工具

【讨论】:

  • 我认为:“选错太容易了”可以在这个答案中加粗。由于选择了错误的版本,我已经失去了一整天。我还想补充一点,匹配 86x/64x WAMP 和 memcache.dll 版本以及 WAMP 编译的 VC6/VC9 和匹配 VC6/VC9 版本的内存缓存是非常重要的.dll。我有 WAMP VC9,而我使用的是 memcache.dll VC6,这就是它不起作用的原因。
  • @Rob - 好点!我已经编辑了答案并突出显示了答案的重要部分。
  • 为了确定是使用线程安全还是非线程安全,这对我有帮助:*.com/a/5800346/470749
  • 我对此感到非常困惑。 Memcached(带有 'd')和 Memcache 不是一回事。过去几个小时我在网上阅读的所有其他帖子都表明 Memcached(以“d”结尾)无法安装在 Windows 上。你确定你让 Memcached 在 Win 上工作吗?它可以与 PHP 7.0.1 x64 线程安全一起使用吗?我一直在强调几个小时试图让它发挥作用,但不断得到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.和/或Class 'Memcached' not found
  • 不支持 php 7.2?我看到 5.5 - 5.6?
【解决方案2】:

这是为未来的游客准备的!

  1. 检查 phpinfo() 并查看它是否已列出。
  2. 如果没有,请检查是否在 php.ini 中启用了扩展,然后检查 apache 错误日志 错误信息! dll 应该使用与 php 相同的编译器 是。 (VC9 或 VC6)顺便说一句,memcache.dll 很好

你可以在这里http://downloads.php.net/pierre/获取php扩展“memcache”在windows上使用memcached和php

Memcached 是服务器守护进程,您可以在此处为 Windows 获取它http://splinedancer.com/memcached-win32/

【讨论】:

  • 所有链接似乎都被破坏了:|
  • 还是坏了!请修复
【解决方案3】:

基于 cmets,我假设您尚未下载并安装 memcached,但已经成功安装了 PHP 的 memcached 模块。基本上,你已经拿到了车钥匙,但没有车。

memcached 是为 Linux 构建的,但它已被其他人移植到 Windows。本教程很旧,但它可能是您正在寻找的内容:http://www.codeforest.net/how-to-install-memcached-on-windows-machine

【讨论】:

    【解决方案4】:

    您的composer.json 应该在其中列出ext-memcached,但它不会安装,如果它丢失它只会抛出一个错误。以下是获得它的各种方法:

    Windows 二进制路由

    截至 2018 年,AFAIK 没有适用于 PHP 7 的 JUST Memcached 的二进制 Windows 端口 但是在LaragonWinginx 中有一个预打包的版本

    Windows DLL 路由

    handful 的人在 github 上提供 compiled DLLs(64 位,并提供线程安全)

    Linux 路由的 Windows 子系统

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

    如果使用的话重启php fpmsudo service php7.2-fpm restart

    从源路径编译

    You can compile the php bindingswindows package of memcached 已损坏 4 年(截至 2018 年)

    仅本地缓存文件 Polyfill 路由

    这是一个名为 StaticCache 的 Memcached 脏包装器,您可以在紧要关头使用它从磁盘读取/写入值。它显然比 memcached 慢得多,所以它只是作为 Windows 开发的鞋垫。如果您喜欢,可以将其定义为同名的 polyfill

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

    【讨论】: