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
3
Answers
Check your
memcache.redundancy
memcache.redundancysetting, your data is not be available on every memcached node. Setting it to 3 should be sufficient in your example case.
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:
Therefore in PHP client you could try a consistent hashing:
From: http://www.php.net/manual/en/memcached.constants.php
Also, try:
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