用于性能优化的二级缓存配置
L2(两级)缓存通过在每个Web节点上添加本地缓存层,减少了远程缓存存储(Redis或Valkey)与Commerce应用程序之间的网络流量。 每个请求的标准Commerce实例传输大约300 KB,在某些情况下,流量可能会快速增长到1000多个请求。
通过二级缓存,每个Web节点将经常访问的数据存储在本地,并将远程缓存用于两个目的:
- 检查缓存数据版本,确保最新的缓存存储在本地
- 正在将更新的缓存数据从远程存储传输到本地计算机
Commerce会将经过哈希处理的数据版本存储在远程缓存中,并将后缀:hash附加到常规键中。 当本地缓存过期时,将通过缓存适配器从远程计算机中获取数据。
有两种可用的二级缓存实施:
旧版L2缓存配置(RemoteSynchronizedCache)
使用以下示例修改或替换app/etc/env.php文件中的现有缓存部分。
'cache' => [
'frontend' => [
'default' => [
'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
'backend_options' => [
'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
'remote_backend_options' => [
'persistent' => 0,
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
],
'local_backend' => 'Cm_Cache_Backend_File',
'local_backend_options' => [
'cache_dir' => '/dev/shm/'
]
],
'frontend_options' => [
'write_control' => false,
],
]
],
'type' => [
'default' => ['frontend' => 'default'],
],
],
其中:
-
backend是二级缓存实现。 -
backend_options是二级缓存配置。remote_backend是远程缓存实现: Redis或MySQL。remote_backend_options是远程缓存配置。local_backend是本地缓存实现:Cm_Cache_Backend_Filelocal_backend_options是本地缓存配置。cache_dir是用于存储本地缓存的目录的文件缓存特定选项。
Adobe建议使用Redis进行远程缓存(\Magento\Framework\Cache\Backend\Redis),使用Cm_Cache_Backend_File进行共享内存中数据的本地缓存,使用: 'local_backend_options' => ['cache_dir' => '/dev/shm/']
Adobe建议使用cache preload功能,因为它可显着降低Redis上的压力。 不要忘记为预加载密钥添加后缀“:hash”。
过时的缓存选项
从Commerce 2.4开始,use_stale_cache选项通过在并行进程中生成新缓存数据时提供以前缓存的数据,可以在特定情况下提高性能。
通常,从性能角度来看,锁定等待的权衡是可以接受的。 但是,随着块数或缓存条目的增加,锁定需要更多时间。 在某些情况下,等待时间最多可以为进程的键数 x 查找超时。 在极少数情况下,商家的Block/Config缓存中可能有数百个键,因此,即使是较小的锁查找超时也可能需要几秒钟。
'use_stale_cache' => true添加到二级缓存前端的最上层配置中。Adobe建议仅对从中获益最大的缓存类型启用use_stale_cache选项,包括:
block_htmlconfig_integration_apiconfig_integrationfull_pagelayoutreflectiontranslate
Adobe不建议为default缓存类型启用use_stale_cache选项。
以下代码显示了示例配置:
'cache' => [
'frontend' => [
'default' => [
'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
'backend_options' => [
'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
'remote_backend_options' => [
'persistent' => 0,
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
],
'local_backend' => 'Cm_Cache_Backend_File',
'local_backend_options' => [
'cache_dir' => '/dev/shm/'
]
],
'frontend_options' => [
'write_control' => false,
],
],
'stale_cache_enabled' => [
'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
'backend_options' => [
'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
'remote_backend_options' => [
'persistent' => 0,
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
],
'local_backend' => 'Cm_Cache_Backend_File',
'local_backend_options' => [
'cache_dir' => '/dev/shm/'
],
'use_stale_cache' => true,
],
'frontend_options' => [
'write_control' => false,
],
]
],
'type' => [
'default' => ['frontend' => 'default'],
'layout' => ['frontend' => 'stale_cache_enabled'],
'block_html' => ['frontend' => 'stale_cache_enabled'],
'reflection' => ['frontend' => 'stale_cache_enabled'],
'config_integration' => ['frontend' => 'stale_cache_enabled'],
'config_integration_api' => ['frontend' => 'stale_cache_enabled'],
'full_page' => ['frontend' => 'stale_cache_enabled'],
'translate' => ['frontend' => 'stale_cache_enabled']
],
],
现代Symfony L2缓存实施
[2.4.9-beta]{class="badge negative" title="仅在2.4.9 Beta版中提供。"}
从Commerce 2.4.9开始,您可以使用基于Symfony缓存的二级缓存实现(symfony_l2后端),该实现提供了新型、符合PSR-6的缓存实现,其性能比传统RemoteSynchronizedCache有显着改进。
Symfony二级缓存的优势
- 现代体系结构:基于Symfony缓存组件构建(符合PSR-6)
- 更好的性能:对Igbinary序列化、gzip压缩和Lua脚本的本机支持
- 永久连接:通过连接池减少Redis或Valkey连接开销
- 预加载密钥:支持为关键数据预加载缓存密钥
- 过时缓存支持:与
use_stale_cache选项完全兼容 - 简化的配置:清理器后端类型名称(
redis、valkey、file)
Symfony L2缓存的配置示例
为L2缓存使用简化的symfony_l2后端类型:
'cache' => [
'frontend' => [
'default' => [
'backend' => 'symfony_l2',
'backend_options' => [
// L2 (Remote): Redis with Symfony Cache
'remote_backend' => 'redis',
'remote_backend_options' => [
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'password' => '',
'serializer' => 'igbinary',
'compression_lib' => 'gzip',
'persistent_id' => 'magento_l2_default',
'timeout' => '2.5',
'read_timeout' => '2.0',
'use_lua' => '1',
'preload_keys' => [
'prefix_EAV_ENTITY_TYPES:hash',
'prefix_GLOBAL_PLUGIN_LIST:hash',
'prefix_DB_IS_UP_TO_DATE:hash',
'prefix_SYSTEM_DEFAULT:hash',
],
],
// L1 (Local): File cache
'local_backend' => 'file',
'local_backend_options' => [
'cache_dir' => '/dev/shm/magento_l1'
],
'cleanup_percentage' => 90,
],
]
],
'type' => [
'default' => ['frontend' => 'default'],
],
],
Symfony L2缓存和过时的缓存
配置单独的前端以支持过时的缓存:
'cache' => [
'frontend' => [
// Default frontend: NO stale cache
'default' => [
'backend' => 'symfony_l2',
'backend_options' => [
'remote_backend' => 'redis',
'remote_backend_options' => [
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'serializer' => 'igbinary',
'compression_lib' => 'gzip',
'persistent_id' => 'magento_l2_default',
],
'local_backend' => 'file',
'local_backend_options' => [
'cache_dir' => '/dev/shm/magento_l1'
],
],
],
// Stale cache enabled frontend
'stale_cache_enabled' => [
'backend' => 'symfony_l2',
'backend_options' => [
'remote_backend' => 'redis',
'remote_backend_options' => [
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'serializer' => 'igbinary',
'compression_lib' => 'gzip',
'persistent_id' => 'magento_l2_stale',
],
'local_backend' => 'file',
'local_backend_options' => [
'cache_dir' => '/dev/shm/magento_l1_stale'
],
'use_stale_cache' => true,
],
]
],
'type' => [
'default' => ['frontend' => 'default'],
'layout' => ['frontend' => 'stale_cache_enabled'],
'block_html' => ['frontend' => 'stale_cache_enabled'],
'reflection' => ['frontend' => 'stale_cache_enabled'],
'config_integration' => ['frontend' => 'stale_cache_enabled'],
'config_integration_api' => ['frontend' => 'stale_cache_enabled'],
'full_page' => ['frontend' => 'stale_cache_enabled'],
'translate' => ['frontend' => 'stale_cache_enabled'],
],
],
Symfony L2缓存的后端选项
remote_backend'redis'redis、valkey或fileremote_backend_options[]local_backend'file'file或apculocal_backend_options[]cleanup_percentage90use_stale_cachefalseValkey支持
symfony_l2后端还支持将Valkey作为远程后端:
'backend_options' => [
'remote_backend' => 'valkey', // Use Valkey instead of Redis
'remote_backend_options' => [
'server' => 'localhost',
'database' => '0',
'port' => '6379',
'serializer' => 'igbinary',
'compression_lib' => 'gzip',
],
// ... rest of configuration
]
有关详细的配置选项,请参阅: