cyberhull / magento2-module-cybercache
使用 CyberCache 集群实现的通用和页面缓存后端
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2024-09-29 05:29:47 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 扩展即使不这样做也可以正常工作。扩展的 "组件名称"(如果您选择启用它)是
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' => '\\CyberHULL\\CyberCache\\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 会话存储。
自 Magento 2 的首次发布以来,就提供了 Redis 会话扩展,但它直到版本
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) 更新自动加载映射
在 Magento 根目录下运行以下命令(作为 magento
用户!)
composer dump-autoload
有关如何安装/使用 composer 的说明,请参见上面的第 1.3 节。
可以通过在 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
作为其会话存储运行。