简体   繁体   English

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

[英]Memcache PHP extension on windows 10 works intermittently

I have memcache extensions on apache/php windows 10 (details at the end) 我在apache / php windows 10上有memcache扩展(最后的细节)

Created a simple test: 创建了一个简单的测试:

$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);

the memcache servers are on AWS and they work well (production servers). memcache服务器在AWS上运行良好(生产服务器)。 This test code works - it retreives the value from the memcache servers. 此测试代码有效 - 它从内存缓存服务器中检索值。 However if I wait a couple minutes and refresh it will not return value. 但是,如果我等待几分钟并刷新它将不会返回值。 Then if I refresh again it will return the value. 然后如果我再次刷新它将返回值。

The same code/config works from another dev computer. 相同的代码/配置适用于另一台开发计算机。

What can cause this? 是什么导致这个?

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

The memcached service doesn't actually install the PHP memcached extension for you. memcached服务实际上并不为您安装PHP memcached扩展。 It only installs the memcached server used to store your cache. 它仅安装用于存储缓存的memcached服务器。

You'll need to download the Windows DLL from the PECL repository first (click on the blue Windows DLL link). 您需要首先从PECL存储库下载Windows DLL(单击蓝色Windows DLL链接)。 Then you must add the extension=php_memcache.dll line to the correct php.ini file for your SAPI. 然后,您必须将extension = php_memcache.dll行添加到SAPI的正确php.ini文件中。 Also, note the extension DLL file needs to be placed in the correct path for your XAMPP installation. 另请注意,扩展DLL文件需要放在XAMPP安装的正确路径中。

For Apache, simply create a script in your document root with the line 对于Apache,只需在文档根目录中创建一个脚本即可

For the CLI SAPI, you can use php.exe --ini to do the same. 对于CLI SAPI,您可以使用php.exe --ini执行相同操作。 Again, you may need to rely on the XAMPP package if it has modified your configuration path (since this is a compile time directive). 同样,如果它修改了您的配置路径,您可能需要依赖XAMPP包(因为这是一个编译时指令)。

After making your changes to php.ini you will need to restart PHP for the changes to take effect. 在对php.ini进行更改后,您需要重新启动PHP才能使更改生效。

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

Since you're using PHP 7 on Windows it's probably also important to note that the compiled DLL from PECL may not actually work under apache for Windows, because you're more than likely using a theaded SAPI. 由于您在Windows上使用PHP 7,因此很可能还需要注意,来自PECL的已编译DLL实际上可能无法在Windows的apache下运行,因为您很可能使用了串行的SAPI。 So make sure you are downloading the correct version. 因此,请确保您下载的是正确的版本。 As far as I can tell that version is only compiled to work with up to PHP 5.6. 据我所知,该版本只能编译为高达PHP 5.6。 The github alternative, for PHP 7, available at https://github.com/nono303/PHP7-memcahe-dll as mentioned in the comments is tested under non-thread safe. 对于PHP 7的github替代方案,可在https://github.com/nono303/PHP7-memcahe-dll中获得,如评论中所述,在非线程安全下进行测试。 So you may only be able to get this working for your CLI scripts on Windows. 因此,您可能只能在Windows上使用CLI脚本。

The problem seems to be more in the way you are writing ( set ) and getting ( get ) the data having multiple nodes. 问题似乎更多的是你编写( set )和获取( get )具有多个节点的数据的方式。 - Memcache does not support replication. - Memcache不支持复制。

Try a single node, in that case, you should be available to get the data just after being set. 尝试单个节点,在这种情况下,您应该可以在设置后获取数据。

When having multiple nodes " sharding " is a common approach to store the data, this means that a logic is implemented in place to determine what server to use for either writing or getting the data: 当具有多个节点“ 分片 ”是存储数据的常用方法时,这意味着实施逻辑以确定用于写入或获取数据的服务器:

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

Clients use client-side libraries to contact the servers which, by default, expose their service at port 11211. Both TCP and UDP are supported. 客户端使用客户端库联系服务器,默认情况下,这些服务器在端口11211上公开其服务。支持TCP和UDP。 Each client knows all servers; 每个客户都知道所有服务器 the servers do not communicate with each other. 服务器不相互通信。 If a client wishes to set or read the value corresponding to a certain key, the client's library first computes a hash of the key to determine which server to use. 如果客户端希望设置或读取对应于某个密钥的值,则客户端的库首先计算密钥的散列以确定使用哪个服务器。 This gives a simple form of sharding and scalable shared-nothing architecture across the servers. 这为服务器提供了一种简单的分片形式和可扩展的无共享架构。

Therefore in PHP client you could try a consistent hashing: 因此,在PHP客户端中,您可以尝试一致的哈希:

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

From: http://www.php.net/manual/en/memcached.constants.php 来自: 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.

Also, try: 另外,尝试:

 memcache.hash_strategy = consistent;

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

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

Check your memcache.redundancy memcache.redundancy setting, your data is not be available on every memcached node. 检查memcache.redundancy memcache.redundancy设置,您的数据在每个memcached节点上都不可用。 Setting it to 3 should be sufficient in your example case. 在您的示例中,将其设置为3就足够了。

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

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