如何在Magneto 2中配置Redis缓存

本文演示如何配置的Redis为Magento的2page and session。它包含以下部分:

  • 步骤1.将Redis用于Magento 2的优势。
  • 步骤2.将Redis用于Magento页面和默认缓存。
  • 步骤3.验证Redis和Magento条件。

将Redis用于Magento 2的优势:

  • Redis也可以用于PHP会话存储,从而可以用Redis完全替换memcached。
  • Redis后端通过索引文件中的标签来工作,因此标签操作不需要对每个缓存文件进行全面扫描。
  • 元数据和缓存记录存储在同一个文件中,而不是存储在单独的文件中,从而导致更少的inode和更少的文件状态,读取,写入,锁定和取消链接操作。而且,由于adler32哈希算法和前缀,原始的哈希目录结构分布很差。多级嵌套目录已被删除,以支持由多个字符组成的单级嵌套。
  • 后端支持基于标记的缓存清理,而无需foreach循环。

将Redis用于Magento页面和默认缓存

Magento的缓存由Magento \ Framework \ App \ CacheInterface实现

将类似于以下的配置添加到 app/etc/env.php

'cache' =>
array(
   'frontend' =>
   array(
   // Default Cache
      'default' =>
      array(
         'backend' => 'Cm_Cache_Backend_Redis',
         'backend_options' =>
         array(
            'server' => '127.0.0.1',
            'database' => '0',
            'port' => '6379'
            ),
    ),
    // Full page cache
    'page_cache' =>
    array(
      'backend' => 'Cm_Cache_Backend_Redis',
      'backend_options' =>
       array(
         'server' => '127.0.0.1',
         'port' => '6379',
         'database' => '1',
         'compress_data' => '0'
       )
    )
  )
),

代码说明

  • default, page_cache:指定段名称以对所有其他缓存使用特定的段或默认快捷方式。
    默认的缓存段使您能够配置除page_cache(整个页面缓存)以外的所有缓存段。
  • server:Redis服务器的绝对URL,或者127.0.0.1Magento服务器上安装了Redis,或者是UNIX套接字的绝对路径。
  • port:Redis服务器监听端口
  • database:如果将Redis用作默认和完整页面缓存,则为必需。您必须指定其中一个缓存的数据库号;其他缓存默认使用0。
    重要说明:如果将Redis用于多种缓存类型(例如,默认缓存和页面缓存),则数据库号必须不同。
  • password:如果您的Redis服务器需要身份验证,请指定密码。
  • compress_data:仅对于整个页面缓存是必需的。设置为1可压缩整个页面缓存。Redis会根据可用性按以下顺序选择一种压缩算法:snappy,l4z或lzf。如果没有一个可用,Redis将使用gzip。

检查Redis缓存是否正常工作

要验证Redis和Magento是否一起工作,请使用以下命令:

Redis ping命令redis-cli ping
您将PONG在控制台中收到

Redis监视命令redis-cli monitor
刷新店面页面,您将看到类似于以下内容的输出。

如果使用Redis进行页面缓存,则会看到类似于以下内容的输出:

1476826133.810090 [0 127.0.0.1:52366] "select" "1"
1476826133.816293 [0 127.0.0.1:52367] "select" "0"
1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d"
1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d"
1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d"
1476826133.868374 [0 127.0.0.1:52368] "select" "1"
1476826133.869011 [0 127.0.0.1:52369] "select" "0"
1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d"
1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d"
1476826133.879267 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL_PRIMARY_PLUGIN_LIST" "d"
1476826133.883312 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL__EVENT_CONFIG_CACHE" "d"
1476826133.898431 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DB_PDO_MYSQL_DDL_STAGING_UPDATE_1" "d"
1476826133.898794 [0 127.0.0.1:52369] "hget" "zc:k:ea6_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d"
1476826133.905738 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_STORE_DEFAULT_10__235__32__1080MAGENTO2" "d"

... more ...

1476826210.634998 [0 127.0.0.1:52439] "hmset" "zc:k:ea6_MVIEW_CONFIG" "d" "a:18:{s:19:\"design_config_dummy\";a:4:{s:7:\"view_id\";s:19:\"design_config_dummy\";s:12:\"action_class\";s:39:\"Magento\\Theme\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:14:\"customer_dummy\";a:4:{s:7:\"view_id\";s:14:\"customer_dummy\";s:12:\"action_class\";s:42:\"Magento\\Customer\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:13:\"cms_page_grid\";a:4:{s:7:\"view_id\";s:13:\"cms_page_grid\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:1:{s:8:\"cms_page\";a:3:{s:4:\"name\";s:8:\"cms_page\";s:6:\"column\";s:7:\"page_id\";s:18:\"subscription_model\";N;}}}s:21:\"catalog_category_flat\";a:4:{s:7:\"view_id\";s:21:\"catalog_category_flat\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:6:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";N;}s:31:\"catalog_category_entity_decimal\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_decimal\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:27:\"catalog_category_entity_int\";a:3:{s:4:\"name\";s:27:\"catalog_category_entity_int\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:28:\"catalog_category_entity_text\";a:3:{s:4:\"name\";s:28:\"catalog_category_entity_text\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:31:\"catalog_category_entity_varchar\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_varchar\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:32:\"catalog_category_entity_datetime\";a:3:{s:4:\"name\";s:32:\"catalog_category_entity_datetime\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}}}s:24:\"catalog_category_product\";a:4:{s:7:\"view_id\";s:24:\"catalog_category_product\";s:12:\"action_class\";s:46:\"Magento\\Catalog\\Model\\Indexer\\Category\\Product\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:2:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\"

... more ...

如果两个命令均成功,则Redis设置正确。


Warning: Undefined variable $wp in /www/wwwroot/sbboke.com/wp-content/themes/pacify/inc/template-tags.php on line 108

Warning: Attempt to read property "request" on null in /www/wwwroot/sbboke.com/wp-content/themes/pacify/inc/template-tags.php on line 108

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magentomagento2教程magento教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论