繁体   English   中英

Windows 10上的Memcache PHP扩展工作间歇性地工作

[英]Memcache PHP extension on windows 10 works intermittently

我在apache / php windows 10上有memcache扩展(最后的细节)

创建了一个简单的测试:

$memcache = new Memcache;
$memcache->addServer("ext-memcached.e-memcached.xxx.net",11211);
$memcache->addServer("ext-memcached.e-memcached2.xxx.net",11211);
$memcache->addServer("ext-memcached.e-memcached3.xxx.net",11211);

$key='xxx_54921';
$settings = $memcache->get($key);
print_r ($settings);

memcache服务器在AWS上运行良好(生产服务器)。 此测试代码有效 - 它从内存缓存服务器中检索值。 但是,如果我等待几分钟并刷新它将不会返回值。 然后如果我再次刷新它将返回值。

相同的代码/配置适用于另一台开发计算机。

是什么导致这个?

Config:
PHP Version 5.6.34
    Windows NT SPECTRE 6.2 build 9200 (Windows 8 Home Premium Edition) i586
Build Date  Feb 28 2018 17:45:55
Compiler    MSVC11 (Visual C++ 2012)
Architecture    x86

Memcache extension:
ts x86 version from here:
https://windows.php.net/downloads/pecl/releases/memcache/3.0.8/

memcache info:
memcache support    enabled
Version 3.0.8
Revision    $Revision: 329835 $
Directive   Local Value Master Value
memcache.allow_failover 1   1
memcache.chunk_size 32768   32768
memcache.compress_threshold 20000   20000
memcache.default_port   11211   11211
memcache.hash_function  crc32   crc32
memcache.hash_strategy  standard    standard
memcache.lock_timeout   600 600
memcache.max_failover_attempts  20  20
memcache.protocol   ascii   ascii
memcache.redundancy 1   1
memcache.session_redundancy 2   2

memcached服务实际上并不为您安装PHP memcached扩展。 它仅安装用于存储缓存的memcached服务器。

您需要首先从PECL存储库下载Windows DLL(单击蓝色Windows DLL链接)。 然后,您必须将extension = php_memcache.dll行添加到SAPI的正确php.ini文件中。 另请注意,扩展DLL文件需要放在XAMPP安装的正确路径中。

对于Apache,只需在文档根目录中创建一个脚本即可

对于CLI SAPI,您可以使用php.exe --ini执行相同操作。 同样,如果它修改了您的配置路径,您可能需要依赖XAMPP包(因为这是一个编译时指令)。

在对php.ini进行更改后,您需要重新启动PHP才能使更改生效。

您可以参考: https//pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/

由于您在Windows上使用PHP 7,因此很可能还需要注意,来自PECL的已编译DLL实际上可能无法在Windows的apache下运行,因为您很可能使用了串行的SAPI。 因此,请确保您下载的是正确的版本。 据我所知,该版本只能编译为高达PHP 5.6。 对于PHP 7的github替代方案,可在https://github.com/nono303/PHP7-memcahe-dll中获得,如评论中所述,在非线程安全下进行测试。 因此,您可能只能在Windows上使用CLI脚本。

问题似乎更多的是你编写( set )和获取( get )具有多个节点的数据的方式。 - Memcache不支持复制。

尝试单个节点,在这种情况下,您应该可以在设置后获取数据。

当具有多个节点“ 分片 ”是存储数据的常用方法时,这意味着实施逻辑以确定用于写入或获取数据的服务器:

来自https://en.wikipedia.org/wiki/Memcached

客户端使用客户端库联系服务器,默认情况下,这些服务器在端口11211上公开其服务。支持TCP和UDP。 每个客户都知道所有服务器 服务器不相互通信。 如果客户端希望设置或读取对应于某个密钥的值,则客户端的库首先计算密钥的散列以确定使用哪个服务器。 这为服务器提供了一种简单的分片形式和可扩展的无共享架构。

因此,在PHP客户端中,您可以尝试一致的哈希:

$memcache = new Memcache;
$memcache->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);

来自: http//www.php.net/manual/en/memcached.constants.php

Memcached::OPT_LIBKETAMA_COMPATIBLE

    Enables or disables compatibility with libketama-like behavior. When enabled, 
    the item key hashing algorithm is set to MD5 and distribution is set 
    to be weighted consistent hashing distribution. This is useful because 
    other libketama-based clients (Python, Ruby, etc.) with the same server
    configuration will be able to access the keys transparently.

    Note:

    It is highly recommended to enable this option if you want to use 
    consistent hashing, and it may be enabled by default in future releases.

另外,尝试:

 memcache.hash_strategy = consistent;

查看这篇文章了解更多详情: https//blog.fedecarg.com/2008/12/24/memcached-consistent-hashing-mechanism/

这个答案: https//stackoverflow.com/a/48006009/1135424

检查memcache.redundancy memcache.redundancy设置,您的数据在每个memcached节点上都不可用。 在您的示例中,将其设置为3就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM