簡體   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