简体   繁体   中英

Memcache PHP extension on windows 10 works intermittently

I have memcache extensions on apache/php windows 10 (details at the end)

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). 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. It only installs the memcached server used to store your cache.

You'll need to download the Windows DLL from the PECL repository first (click on the blue Windows DLL link). Then you must add the extension=php_memcache.dll line to the correct php.ini file for your SAPI. Also, note the extension DLL file needs to be placed in the correct path for your XAMPP installation.

For Apache, simply create a script in your document root with the line

For the CLI SAPI, you can use php.exe --ini to do the same. Again, you may need to rely on the XAMPP package if it has modified your configuration path (since this is a compile time directive).

After making your changes to php.ini you will need to restart PHP for the changes to take effect.

You can refer: 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. 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. 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. So you may only be able to get this working for your CLI scripts on Windows.

The problem seems to be more in the way you are writing ( set ) and getting ( get ) the data having multiple nodes. - Memcache does not support replication.

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 :

Clients use client-side libraries to contact the servers which, by default, expose their service at port 11211. Both TCP and UDP are supported. 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:

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

From: 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/

and this answer: https://stackoverflow.com/a/48006009/1135424

Check your memcache.redundancy memcache.redundancy setting, your data is not be available on every memcached node. Setting it to 3 should be sufficient in your example case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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