12

我在 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
4

3 回答 3

5

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 一起使用。如评论中所述,可在https://github.com/nono303/PHP7-memcahe-dll上获得 PHP 7 的 github 替代方案在非线程安全下进行了测试。因此,您可能只能在 Windows 上为您的 CLI 脚本使用此功能。

于 2018-08-02T10:53:28.453 回答
0

问题似乎更多地在于您编写 ( 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

于 2018-08-04T07:47:08.307 回答
-1

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

于 2018-07-30T17:03:04.160 回答