如何在Windows中为PHP安装和使用memcached?

问题描述 投票:9回答:4

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

当我输入wmic进程获取描述时,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?

php memcached
4个回答
3
投票

这是为了未来的访客!

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

你可以得到php扩展“memcache”来在windows上使用memcached与php http://downloads.php.net/pierre/

Memcached是服务器守护进程,你可以在这里获取它的http://splinedancer.com/memcached-win32/


13
投票

任何遇到使用memcached在Windows上工作的问题的人的注意事项。

  • 对于初学者,请确保您拥有正确版本的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 install
  • 使用c:\​​ pathtomemcached \ memcached.exe -d start或net start“memcached Server”执行此操作。在我的安装上,前者不起作用
  • 同样,我无法从任务管理器的“服务”选项卡启动memcached
  • 能够在较低级别使用memcached,以便enable telnet,如果需要,并从命令提示符键入telnet,这很方便。现在打开端口11211并尝试使用memcached
  • 能够密切关注memcached中发生的事情也很有用。 phpMemCacheAdmin是迄今为止最好的工具

3
投票

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

memcached是为Linux而构建的,但它已被其他人移植到Windows。这个教程很旧,但它可能正是你要找的:http://www.codeforest.net/how-to-install-memcached-on-windows-machine


0
投票

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

Windows二进制路由

截至2018年的AFAIK没有用于PHP 7的JUST Memcached的二进制Windows端口但是在Laragon或者Winginx enter image description here中有一个预先打包的版本

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

如果使用sudo service php7.2-fpm restart,请重启php fpm

从源路由编译

You can compile the php bindingswindows package of memcached已被打破4年(截至2018年)

仅本地缓存文件Polyfill路由

这是一个围绕Memcached的脏包装程序,名为StaticCache,您可以在夹点中使用来从磁盘读取/写入值。它显然比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;
}
© www.soinside.com 2019 - 2024. All rights reserved.