文件Commerce設定指南

僅限PaaS

在CentOS上設定記憶體快取

最後更新: 2025年5月5日
  • 主題:
  • 設定
  • 快取

建立對象:

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

本節提供在CentOS上安裝記憶體快取的說明。 如需其他資訊,請參閱memcached wiki。

資訊
Adobe建議使用最新的穩定memcached版本(目前memcached為3.1.3)。

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

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

    CentOS的確切名稱是php-pecl-memcache。

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

    CentOS的確切名稱是php-pecl-memcached。

在CentOS上安裝並設定記憶體快取

若要在CentOS上安裝memcached,請以具有root許可權的使用者身分執行以下工作:

  1. 安裝memcached及其相依性:

    yum -y update
    
    yum install -y libevent libevent-devel
    
    yum install -y memcached
    
    yum install -y php-pecl-memcache
    
    INFO
    上述命令的語法可能取決於您使用的套裝程式儲存區域。 例如,如果您使用webtatic和PHP 5.6,請輸入yum install -y php56w-pecl-memcache。 使用yum search memcache|grep php尋找適當的封裝名稱。
  2. 變更CACHESIZE和OPTIONS的memcached組態設定:

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

    2. 找到CACHESIZE的值,並將其變更為至少1 GB。 例如:

      CACHESIZE="1GB"
      
    3. 找到OPTIONS的值,並將其變更為localhost或127.0.0.1

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

  4. 重新啟動memcached。

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

    若為Apache:

    service httpd restart
    
  6. 繼續下一節。

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

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

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

若要驗證網頁伺服器是否可辨識成員快取:

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

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

    例如, http://192.0.2.1/phpinfo.php

  3. 請確定memcache顯示如下:

確認網頁伺服器可辨識memcache

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

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

建立包含MySQL資料庫和PHP指令碼的memcache測試

此測試使用MySQL資料庫、表格和資料,以確認您可以擷取資料庫資料並將其儲存在memcache中。 PHP指令碼會先搜尋快取。 如果結果不存在,指令碼會查詢資料庫。 在原始資料庫完成查詢之後,指令碼會使用set命令將結果儲存在memcache中。

此測試的詳細資料

建立MySQL資料庫:

mysql -u root -p

在mysql提示下,輸入下列命令:

create database memcache_test;
GRANT ALL ON memcache_test.* TO memcache_test@localhost IDENTIFIED BY 'memcache_test';
use memcache_test;
create table example (id int, name varchar(30));
insert into example values (1, "new_data");
exit

在網頁伺服器的docroot中建立cache-test.php:

$meminstance = new Memcached();

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

$query = "select id from example where name = 'new_data'";
$querykey = "KEY" . md5($query);

$result = $meminstance->get($querykey);

if (!$result) {
   try {
        $dbh = new PDO('mysql:host=localhost;dbname=memcache_test','memcache_test','memcache_test');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $result = $dbh->query("select id from example where name = 'new_data'")->fetch();
        $meminstance->set($querykey, $result, 0, 600);
        print "got result from mysql\n";
        return 0;
    } catch (PDOException $e) {
        die($e->getMessage());
    }
}
print "got result from memcached\n";
return 0;

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

從命令列執行指令碼。

cd <web server docroot>
php cache-test.php

第一個結果是got result from mysql。 這表示該索引鍵不存在於memcached中,但它是從MySQL擷取的。

第二個結果為got result from memcached,可驗證值是否成功儲存在memcached中。

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

telnet localhost <memcache port>

出現提示時,輸入

stats items

結果類似下列:

STAT items:3:number 1
STAT items:3:age 1075
STAT items:3:evicted 0
STAT items:3:evicted_nonzero 0
STAT items:3:evicted_time 0
STAT items:3:outofmemory 0
STAT items:3:tailrepairs 0

清除memcache儲存體並結束Telnet:

flush_all
quit

有關Telnet測試的其他資訊

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