boangri / magento2-module-cybercache
使用 CyberCache 集群实现的通用和页面缓存后端
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2024-09-07 23:54:25 UTC
README
该扩展由两个独立的模块组成:默认缓存/FPC 存储,以及会话存储。
- 默认缓存和 FPC 存储
为了使 Magento 使用 CyberCache 作为其默认缓存以及 FPC 存储,需要执行以下操作:
1.1) 复制扩展文件
将此 readme.md
文件所在的目录中的所有文件以及其所有子目录复制到
<Magento-installation-directory>/app/code/CyberHULL/CyberCache/
如果在该位置已安装 CyberCache 扩展的旧版本,在复制新文件之前可能需要删除旧文件。
重要:文件必须可由 Magento 代码读取!为了使
Apache
服务器能够访问它们,将组所有者更改为www-data
。
安装模块后,可以在 Magento 中启用它,如 Magento 文档 中所述,但这 是可选的:CyberCache 扩展即使没有启用也会工作。扩展的 "component name"(如果您选择启用)是
CyberHULL_CyberCache
;见etc/module.xml
。
1.2) 修改 composer.json
将以下子部分添加到位于 Magento 安装目录中的 composer.json
文件中的 "autoload"
部分
"classmap": [
"app/code/CyberHULL/CyberCache/src/C3"
],
如果已存在 "classmap"
部分,只需将 "app/code/CyberHULL/CyberCache/src/C3"
添加到其末尾(单独记录之间用逗号分隔)。
1.3) 更新自动加载映射
从 Magento 安装目录,以 magento
用户执行以下命令
composer dump-autoload
如果尚未安装
composer
应用程序,可以使用以下方法之一:
在基于 Debian 的系统上使用
sudo apt install composer
安装它,或者使用与 Magento 一起提供的版本;在这种情况下,必须以以下方式运行 composer:
php vendor/composer/composer/bin/composer dump-autoload
可以通过在 vendor/composer/autoload_map.php
中搜索 C3_Store_CyberCacheStore
字符串来检查结果;如果它作为返回数组中的键存在,则 composer 已成功重新配置 Magento。
1.4) 配置 Magento 环境
将以下部分添加到 app/etc/env.php
文件中(如果返回数组中已存在 cache
条目,则必须将其完全替换为以下值)
'cache' =>
array(
'frontend' =>
array(
'default' =>
array(
'backend' => 'C3_Store_CyberCacheStore',
'backend_options' =>
array( // missing entries will get default values
'address' => '127.0.0.1', // connection address; might be an internet address, or an IP
'port' => '8120', // connection port
'persistent' => 'true', // whether to use persistent connections (must match server settings)
'hasher' => 'murmurhash2', // hash method to use for passwords
'admin' => '', // password for administrative commands
'user' => '', // password for user-level commands
'compressor' => 'snappy', // *initial* compressor for various data buffers
'threshold' => '2048', // minimal size of the buffer to compress
'marker' => 'true' // whether to use integrity check marker
),
),
'page_cache' =>
array(
'backend' => 'C3_Store_CyberCacheStore',
'backend_options' =>
array( // missing entries will get default values
'address' => '127.0.0.1', // connection address; might be an internet address, or an IP
'port' => '8120', // connection port
'persistent' => 'true', // whether to use persistent connections (must match server settings)
'hasher' => 'murmurhash2', // hash method to use for passwords
'admin' => '', // password for administrative commands
'user' => '', // password for user-level commands
'compressor' => 'snappy', // *initial* compressor for FPC data buffers
'threshold' => '2048', // minimal size of the buffer to compress
'marker' => 'true' // whether to use integrity check marker
)
)
)
),
完成以上四个步骤后,Magento 2 将运行,其中 CyberCache
是其默认和全页缓存。
- 会话存储
与通用和全页缓存处理程序不同,会话处理程序与 Magento 2 集成更深入,因此需要修补一些 Magento 核心文件才能开始使用 CyberCache 作为 Magento 会话存储。
Redis 会话扩展自 Magento 2 的第一个版本起就可用,但它仅在版本
2.0.6
中得到支持:因为需要修补 Magento 本身。
要启用 CyberCache 作为 Magento 2 会话存储,需要执行以下操作:
2.1) 复制扩展文件
参见上述第 1.1 节。
2.2) 修改核心 Magento 文件
CyberCache 扩展不需要将类包装器添加到 Magento 核心,因为其数据处理方法不会抛出需要捕获的异常(例如,Redis 扩展抛出的 ConcurrentConnectionsExceededException
)。仍然需要做出一些更改。
注意:所有代码更改说明均假定 Magento 2.1.7。行号可能与其他版本不同。
2.2.1) 添加会话保存常量
修改 vendor/Magento/Framework/Config/ConfigOptionsListConstants.php
文件:在60行添加 const SESSION_SAVE_CYBERCACHE = 'cybercache';
。
2.2.2) 使 Magento 识别 CyberCache 为 "用户" 处理器
修改 vendor/Magento/Framework/Session/SaveHandler.php
文件:将第154行的 if ($saveHandler === 'db' || $saveHandler === 'redis') {
改为 if ($saveHandler === 'db' || $saveHandler === 'redis' || $saveHandler === 'cybercache') {
。
2.3) 修改 composer.json
2.3.1) 添加 "psr-4" 记录
在位于 Magento 安装文件夹中的 composer.json
的 "autoload":"psr-4" 部分添加以下行
"C3\\": "app/code/CyberHULL/CyberCache/src/C3"
"psr-4" 部分中的各个条目需要用逗号分隔。
2.3.2) 添加 "psr-0" 记录
在位于 Magento 安装文件夹中的 composer.json
的 "autoload":"psr-0" 部分添加以下行
"C3\\Session\\CyberCacheSession": "app/code/CyberHULL/CyberCache/src/"
"psr-0" 部分中的各个条目需要用逗号分隔。
2.4) 更新 Autoload 映射
在 Magento 根目录下运行以下命令(作为 magento
用户!)
composer dump-autoload
请参阅上文 1.3 节,了解如何安装/使用 composer。
是否更新成功可以通过在 vendor/composer
目录中搜索 autoload_classmap.php
和 autoload_namespaces.php
文件中的字符串 CyberCacheSession
来检查;另外,字符串 CyberCache
必须出现在同一目录下的 autoload_psr4.php
文件中。
2.5) 配置依赖项
2.5.1) 修改 app/etc/di.xml
通过添加 cybercache
(以下片段中的最后一行 <item>
)来修改 app/etc/di.xml
中的处理器数组
<type name="Magento\Framework\Session\SaveHandlerFactory">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="db" xsi:type="string">Magento\Framework\Session\SaveHandler\DbTable</item>
<item name="redis" xsi:type="string">Magento\Framework\Session\SaveHandler\Redis</item>
<item name="cybercache" xsi:type="string">C3\Session\CyberCacheSession</item>
</argument>
</arguments>
</type>
2.5.1) 修改 vendor/magento/magento2-base/app/etc
通过添加 cybercache
(以下片段中的最后一行 <item>
)来修改 vendor/magento/magento2-base/app/etc
中的处理器数组
<type name="Magento\Framework\Session\SaveHandlerFactory">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="db" xsi:type="string">Magento\Framework\Session\SaveHandler\DbTable</item>
<item name="redis" xsi:type="string">Magento\Framework\Session\SaveHandler\Redis</item>
<item name="cybercache" xsi:type="string">C3\Session\CyberCacheSession</item>
</argument>
</arguments>
</type>
2.6) 重新编译 DI 文件
在 Magento 安装文件夹中,作为 magento
用户执行以下命令
bin/magento setup:di:compile
编译应以 Generated code and dependency injection configuration successfully.
消息结束。
2.7) 配置 Magento 环境
默认的 Magento 环境配置位于 app/etc/env.php
,其中包含以下条目
'session' =>
array (
'save' => 'files',
),
要配置基于 CyberCache 的会话存储,必须将 app/etc/env.php
中的 session
部分修改为以下内容
'session' =>
array (
'save' => 'cybercache', // replaces 'files'
'cybercache' =>
array ( // missing entries will get default values (the array might as well be empty)
'address' => '127.0.0.1', // connection address; might be an internet address, or an IP
'port' => '8120', // connection port
'persistent' => 'true', // whether to use persistent connections (must match server settings)
'hasher' => 'murmurhash2', // hash method to use for passwords
'admin' => '', // password for administrative commands
'user' => '', // password for user-level commands
'compressor' => 'snappy', // *initial* compressor for various data buffers
'threshold' => '2048', // minimal size of the buffer to compress
'marker' => 'true', // whether to use integrity check marker
'magento_lifetime' => 'all' // whether to honor lifetimes set in Magento
)
),
完成以上七个步骤后,Magento 2 将使用 CyberCache
作为其会话存储运行。