Magento在工作时需要暂用用大量内存,这也是Magento 2安装需求必须内存是2g,这样才能保住Magento 2系统完美运行不至于瘫痪,并且有时候需要在一天内多次刷新内存。很多时候,Magento开发人员需要为特定的cms页面刷新缓存,因为cms页面发生了更改,或者从cms页面调用另一个phtml文件,或者你在这个phtml文件更改了代码等等。因此,在这之后您需要直接刷新缓存,对于这种刷新,整个站点缓存是不合适的,因为它将导致站点的反应时间加剧,整站刷新这不是我们推荐的。因此,要养成只刷新特定cms页面的习惯,而不影响整个站点。下面我们要知道Magento 2的缓存
Magento 2缓存
Magento 2有以下缓存类型
缓存类型 | 缓存的code | 描述 |
Configuration | config | Magento从所有模块收集配置,合并配置,并将合并后的结果保存到缓存中。此缓存还包含存储在文件系统和数据库中的特定于存储库的设置。在修改配置文件后清除或刷新此缓存类型。 |
Layout | layout | 已编译的页面布局(即来自所有组件的布局组件[layout])。在修改布局文件后清除或刷新此缓存类型。 |
Block HTML output | block_htmlblock_html | 每个块的HTML页面片段。在修改视图层之后清除或刷新这个缓存类型。 |
Collections data | collections | 数据库查询的结果。如果需要,Magento会自动清理这个缓存,但是第三方开发人员可以将任何数据放入缓存的任何段中。如果您的自定义模块使用导致Magento无法清除的缓存条目的逻辑,则清除或刷新此缓存类型。 |
DDL | db_ddl | 数据库模式。如果需要,Magento会自动清理这个缓存,但是第三方开发人员可以将任何数据放入缓存的任何段中。在对数据库模式进行自定义更改之后,清除或刷新此缓存类型。(换句话说,就是Magento自己不做的更新。)自动更新数据库模式的一种方法是使用magento setup:db-schema:upgrade命令。 |
Compiled Config | compiled_config | 编译配置 |
Entity attribute value (EAV) | eav | 与EAV属性相关的元数据(例如,存储标签、到相关PHP代码的链接、属性呈现、搜索设置等)。您通常不需要清除或刷新此缓存类型 |
Page cache | full_page | 生成的HTML页面。如果需要,Magento会自动清理这个缓存,但是第三方开发人员可以将任何数据放入缓存的任何段中。在修改影响HTML输出的代码级别后清除或刷新此缓存类型。建议保持启用此缓存,因为缓存HTML可以显著提高性能。 |
Reflection | reflection | 移除Webapi模块和客户模块之间的依赖项。 |
Translations | translate | 合并所有模块的翻译后,合并缓存将被清理。 |
Integration configuration | config_integration | 编译的集成。在更改或添加集成之后清除或刷新此缓存。 |
Integration API configuration | config_integration_api | 编译了商店集成的集成api配置 |
Web services configuration | config_webservice | 缓存Web API结构。 |
Customer Notification | customer_notification | 出现在用户界面中的临时通知。 |
查看缓存状态
1 | bin/magento cache:status |
如下例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | config: 1 layout: 1 block_html: 1 collections: 1 reflection: 1 db_ddl: 1 compiled_config: 1 eav: 1 customer_notification: 1 config_integration: 1 config_integration_api: 1 google_product: 1 full_page: 1 config_webservice: 1 translate: 1 vertex: 1 |
如果你不知道Magento 2的命令请点击这里阅读:Magento 2控制台命令总结
清除CMS缓存页面
接下来我将给大家讲解下如何使用代码的形式进行特定的cms页面清理缓存。
我们必须创建一个PHP脚本,您需要将其放在已安装的Magento文件夹的根目录中,然后可以直接在web浏览器上运行。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php use Magento\Framework\AppInterface; try { require __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try { $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend'); $fullPageCache = $objectManager->get(\Magento\PageCache\Model\Cache\Type::class); $fullPageCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('cms_p_2')); // Here 2 is a cms page id here you can pass any cms page id } catch(\Exception $e){ echo "Error :"; echo $e->getMessage(); } |
通过上面代码,您将能够成功地刷新特定的CMS页面。
清除/刷新所有缓存程序
在开发的情况下,开发人员或商人的请求,他/她需要清除/刷新缓存的程序。下面实现这些代码行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php use Magento\Framework\App\PageCache\Version; use Magento\Framework\App\Cache\TypeListInterface; use Magento\Framework\App\Cache\Frontend\Pool; protected $cacheTypeList; protected $cacheFrontendPool; public function __construct( TypeListInterface $cacheTypeList, Pool $cacheFrontendPool ){ $this->cacheTypeList = $cacheTypeList; $this->cacheFrontendPool = $cacheFrontendPool; } public function flushCache(Version $subject) { $_types = [ 'config', 'layout', 'block_html', 'collections', 'reflection', 'db_ddl', 'eav', 'config_integration', 'config_integration_api', 'full_page', 'translate', 'config_webservice' ]; foreach ($_types as $type) { $this->cacheTypeList->cleanType($type); } foreach ($this->cacheFrontendPool as $cacheFrontend) { $cacheFrontend->getBackend()->clean(); } } |
通过上面可以清除所有的缓存。你可以在控制器 controller或模型 model 中调用flushCache()函数。以上两种方法是手动清除的好方法希望对你有帮助。
非常不错啊,期待更多好内容!