文件Commerce設定指南

僅限PaaS

在Ubuntu上設定memcached

2025年5月5日
  • 主題:
  • 設定
  • 快取

建立對象:

  • 經驗豐富
  • 管理員
  • 開發人員

本節提供在Ubuntu上安裝memcached的說明。

INFO
Adobe建議使用memcached 3.0.5版或更新版本。

因為PHP對memcache沒有原生支援,所以您必須安裝擴充功能以供PHP使用。 有兩個可用的PHP擴充功能,請務必解碼要使用哪個:

  • memcache (no d) — 不是定期維護的舊版但常用的擴充功能。
    memcache延伸模組目前​ 不適用於PHP 7。 請參閱memcache🔗的PHP檔案。

    Ubuntu的確切名稱是php5-memcache。

  • memcached (具有d) — 與PHP 7相容的較新且已維護的擴充功能。 請參閱memcached🔗的PHP檔案。

    Ubuntu的確切名稱是php5-memcached。

在Ubuntu上安裝並設定memcached

若要在Ubuntu上安裝並設定memcached:

  1. 以具有root許可權的使用者身分,輸入下列命令:

    apt-get -y update
    
    apt-get -y install php5-memcached memcached
    
  2. 變更CACHESIZE和-l的memcached組態設定:

    1. 在文字編輯器中開啟/etc/memcached.conf。

    2. 找到-m引數。

    3. 將其值變更為至少1GB

    4. 找到-l引數。

    5. 將其值變更為127.0.0.1或localhost

    6. 將變更儲存至memcached.conf並結束文字編輯器。

    7. 重新啟動memcached。

      service memcached restart
      
  3. 重新啟動您的網頁伺服器。

    針對Apache,service apache2 restart

  4. 繼續下一節。

在安裝Magento之前驗證memcached的運作方式

Adobe建議先測試memcached以確保其可運作,然後再安裝Commerce。 只需幾分鐘即可完成這項工作,並可簡化後續的疑難排解。

驗證Web伺服器是否可辨識memcached

若要確認網頁伺服器可辨識memcached:

  1. 在網頁伺服器的docroot中建立phpinfo.php檔案:

    <?php
    // Show all information, defaults to INFO_ALL
    phpinfo();
    
  2. 前往網頁瀏覽器中的該頁面。 例如:

    http://192.0.2.1/phpinfo.php
    
  3. 請確定memcached顯示如下:

    確認網頁伺服器可辨識memcached

    確認您使用的是memcached 3.0.5版或更新版本。

    如果memcached未顯示,請重新啟動網頁伺服器並重新整理瀏覽器頁面。 如果仍然未顯示,請確認您已安裝php-pecl-memcached擴充功能。

驗證memcached可以快取資料

此測試使用PHP指令碼來驗證memcached可以儲存和擷取快取資料。

如需此測試的詳細資訊,請參閱如何在Ubuntu教學課程中安裝及使用Memcache。

在網頁伺服器的docroot中建立cache-test.php,其內容如下:

$meminstance = new Memcached();

$meminstance->addServer("<memcached hostname or ip>", <memcached port>);

$result = $meminstance->get("test");

if ($result) {
    echo $result;
} else {
    echo "No matching key found. Refresh the browser to add it!";
    $meminstance->set("test", "Successfully retrieved the data!") or die("Could not save anything to memcached...");
}

其中<memcached hostname or ip>是localhost、127.0.0.1或memcache主機名稱或IP位址。 <memcached port>是接聽連線埠;預設為11211。

在網頁瀏覽器中前往該頁面。 例如

http://192.0.2.1/cache-test.php

第一次前往頁面時,會顯示下列專案: No matching key found. Refresh the browser to add it!

重新整理瀏覽器。 訊息變更為Successfully retrieved the data!

最後,您可以使用Telnet檢視memcache金鑰:

telnet localhost <memcache port>

出現提示時,輸入

stats items

結果類似下列:

STAT items:2:number 1
STAT items:2:age 106
STAT items:2:evicted 0
STAT items:2:evicted_nonzero 0
STAT items:2:evicted_time 0
STAT items:2:outofmemory 0
STAT items:2:tailrepairs 0
STAT items:2:reclaimed 0
STAT items:2:expired_unfetched 0
STAT items:2:evicted_unfetched 0

排清成員快取儲存體並結束Telnet:

flush_all
quit

有關Telnet測試的其他資訊

recommendation-more-help
386822bd-e32c-40a8-81c2-ed90ad1e198c