Magento 2 Cache Guide

Magento 2 Cache Guide

Introduction

Caching is an inseparable part in any systems, especially for a complex and data-dependent one like Magento 2. Almost everything in Magento has its own cache: modules, themes, etc.. Meanwhile, improper cache management may turn your developing experience into a living hell. So we will have a quick look on Magento 2 cache directories and how to clear them.

Types of Magento 2 Cache

Generally almost all Magento caches are stored in var/ directory. Specifically:

  • var/page_cache: generated from full page cache
  • var/cache: contains cachable objects except the page cache
  • var/composer_home: Setup Wizard home directory. In most cases, you shouldn’t touch this directory.
  • var/generation: contains generated code by factory classes, proxy generated objects, and interceptor classes .
  • var/di: contains compiled dependency injection configuration for all modules
  • var/view_preprocessed: contains minified templates and compiled styling files
Magento 2 Cache Management Grid
Magento 2 Cache Management Grid

Cleaning the caches

First of all, as a developer, it is recommended that you turn off all caching by command bin/magento cache:disable

In addition, for admins, there’s a caching interface in System -> Cache Management where you can clear the caches within a click of a button, but as a developer, there are a few tricks that you can use:

  • Clear the cache with command bin/magento cache:clean
    List of cleaned caches
    List of cleaned caches
  • Depending on what type of code you modified, there are different ways that you can clear the cache. Hence, just go for rm -rf <cache directory>
    • Change a block or controller’s construct method, or any class that has a plugin related to it: var/di, var/generation
    • Change js code: var/view_processed, var/cache, var/page_cache. You will also have to take browser cache into account, since browsers always have their caches for better performance. Sometimes a developer goes nuts on javascript just because he forgot the browser cache, including myself.
    • Add, remove, enable or disable module: var/di, var/generation, var/cache, var/page_cache.  Alternatively, you can always use upgrading command bin/magento setup:upgrade for this case.

That’s all to caching for now. Effectively manage this and it will save you from a sky of trouble as a result. 
[magenest_display_products skus=”alipay-cross-border-payment-gateway”]

Relevant Entries
How To Configure Yahoo API in Magento 2?20170922200047
How To Configure Amazon API In Magento 220171002200046
Sell tickets and manage events on Magento 2. How can I do?20151219091145