如何在 Windows 中为 PHP 安装和使用 memcached?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14777857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install and use memcached in Windows for PHP?
提问by Koray Tugay
I have installed memcached binary file in Windows 7 and started it as server.
我已经在 Windows 7 中安装了 memcached 二进制文件并将其作为服务器启动。
When I type wmic process get description, exetuablepath | findstr memcached.exe I get the response: memcached.exe c:\memcached\memcached.exe on command line.
当我输入 wmic process get description, exetuablepath | findstr memcached.exe 我在命令行上得到响应:memcached.exe c:\memcached\memcached.exe。
When I try running the sample code on php.net, I get on my browser:
当我尝试在 php.net 上运行示例代码时,我进入了我的浏览器:
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
致命错误:在第 3 行的 C:\DocumentRoot\Framework\index.php 中找不到类“Memcache” 调用堆栈: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?
那么,我做错了什么?我正在使用 memcache.dll,因为我相信 Windows 不存在 memcached.dll?
采纳答案by Venu
This is for future vistors!
这是给未来的游客!
- check phpinfo() and see if it's listed.
- 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
- 检查 phpinfo() 并查看它是否已列出。
- 如果没有,请检查 php.ini 中是否启用了扩展,然后检查 apache 错误日志以获取错误消息!dll 应该使用与 php 相同的编译器。(VC9 或 VC6)顺便说一句,memcache.dll 很好
You can get the php extension "memcache" to use memcached with php on windows here http://downloads.php.net/pierre/
您可以在 Windows 上获取 php 扩展“memcache”以使用带有 php 的 memcached http://downloads.php.net/pierre/
Memcached is the server daemon and you can get it for windows here http://splinedancer.com/memcached-win32/
Memcached 是服务器守护进程,您可以在此处为 Windows 获取它http://splinedancer.com/memcached-win32/
回答by DroidOS
A note to anyone who runs into issues with getting memcached working on Windows.
给在 Windows 上使用 memcached 遇到问题的任何人的说明。
- 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 promptrun 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 startor 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. phpMemCacheAdminis by far the best tool for the job
- 对于初学者,请确保您拥有正确版本的 memcached dll 并且它是可访问的。http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/提供了广泛的选择,而且很容易选择错误的 memcached 版本!.
- 如果您运行的是 PHP 5.5,您还需要 php5.dll。你可以在这里得到这个
- 您可能需要编辑您的环境 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 很方便,因此如果需要,请启用 telnet,并从命令提示符键入 telnet。现在打开端口 11211 并尝试使用 memcached
- 能够密切关注 memcached 中发生的事情也很有用。phpMemCacheAdmin是迄今为止最好的工作工具
回答by Michael Marr
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,但已成功安装了 PHP 的 memcached 模块。基本上,你已经拿到了车钥匙,但没有车。
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
memcached 是为 Linux 构建的,但它已被其他人移植到 Windows。本教程很旧,但它可能是您正在寻找的内容:http: //www.codeforest.net/how-to-install-memcached-on-windows-machine
回答by Jonathan
Your composer.jsonshould have ext-memcachedlisted in it but it won't install, it'll just throw an error if it's missing. Here's various ways to get it:
您composer.json应该在其中ext-memcached列出但它不会安装,如果缺少它只会引发错误。以下是获得它的各种方法:
Windows Binary Route
Windows 二进制路由
AFAIK as of 2018 there is no binary Windows port of JUST Memcached for PHP 7
But there is a pre-packaged version in Laragonor alternatively Winginx
AFAIK 截至 2018 年,没有用于 PHP 7 的 JUST Memcached 二进制 Windows 端口但是在Laragon或Winginx 中有一个预打包版本
Windows DLL Route
Windows DLL 路由
There's a handfulof people offering compiled DLLson github (64-bit, and thread-safe offered)
有少数人在 github 上提供已编译的 DLL(提供 64 位和线程安全)
Windows Subsystem For Linux Route
适用于 Linux 路由的 Windows 子系统
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
如果使用它,请重新启动 php fpm sudo service php7.2-fpm restart
Compile from Source Route
从源路由编译
You can compile the php bindingsbut the windows package of memcachedhas been broken for 4 years (as of 2018)
可以编译php绑定但是memcached的windows包已经破解了4年(截至2018年)
Local-Only Cache Files Polyfill Route
本地缓存文件 Polyfill 路由
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
这是一个围绕 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;
}

