Cache overview
Caching is one of the most effective ways to improve website performance. Generally speaking, there are two methods of caching content:
- Client-side (browser)
- Server-side
Retrieving stored (cached) content from a previous request for the same client instead of requesting files from your server every time someone visits your site is a more efficient use of network bandwidth.
The Magento page cache library contains a simple PHP reverse proxy that enables full page caching out of the box. A reverse proxy acts as an intermediary between visitors and your application and can reduce the load on your server.
We recommend using Varnish, but you can use Magento’s default caching mechanism instead, which stores cache files in any of the following:
Cacheable and uncacheable pages
Cacheable and uncacheable are terms we use to indicate whether or not a page should be cached at all. (By default, all pages are cacheable.) If any block in a layout is designated as uncacheable, the entire page is uncacheable.
To create an uncacheable page, mark any block on that page as uncacheable in the layout using cacheable="false"
.
1
2
3
<block class="Magento\Customer\Block\Form\Edit" name="customer_edit" template="Magento_Customer::form/edit.phtml" cacheable="false">
<container name="form.additional.info" as="form_additional_info"/>
</block>
Examples of uncacheable pages include the compare products, cart, checkout pages, and so on.
Do not configure content pages (i.e., catalog, product, and CMS pages) to be uncacheable. Doing so has an adverse affect on performance.
Public and private content
Reverse proxies serve “public” or shared content to more than one user. However, most Magento websites generate dynamic and personalized “private” content that should only be served to one user, which presents unique caching challenges. To address these challenges, Magento can distinguish between two types of content:
-
Public - Public content is stored server side in your reverse proxy cache storage (e.g., file system, database, Redis, or Varnish) and is available to multiple customers. Examples of public content include header, footer, and category listing.
-
Private - Private content is stored client side (e.g., browser) and is specific to an individual customer. Examples of private content include wishlist, shopping cart, customer name, and address. You should limit stored private content to a small portion of the page’s total content.
Cache types
The following cache types mostly have impact on frontend development process:
Cache type “friendly” name | Cache type code name | Description |
---|---|---|
Layout | layout |
Compiled page layouts (that is, the layout components from all components). Clean or flush this cache type after modifying layout files. |
Block HTML output | block_html |
HTML page fragments per block. Clean or flush this cache type after modifying the view layer. |
Page cache | full_page |
Generated HTML pages. If necessary, Magento cleans up this cache automatically, but third-party developers can put any data in any segment of the cache. Clean or flush this cache type after modifying code level that affects HTML output. It’s recommended to keep this cache enabled because caching HTML improves performance significantly. |
Translations | translate |
Merged translations from all modules. |
The full list of cache types can be found in the Overview of cache types topic.
Clean cache
To clean cache, run
1
bin/magento cache:clean <type> ... <type>
To view the status of the cache, run:
1
bin/magento cache:status
For more details about working with cache, see Manage the cache
Clean static files cache
You can clean generated static view files in any of the following ways:
-
In the Admin. Go to System > Tools > Cache Management and click Flush Static Files Cache.
This option is only available in
developer
mode. Refer to the static view files overview for more information. For more details about the Magento modes, see Magento Modes -
Manually by clearing the
pub/static
andvar/view_preprocessed
directories and subdirectories except forpub/static/.htaccess
.To clear the
pub/static
directory of all files except.htaccess
(which is a hidden file), enter the following command:1
rm -r pub/static/*/*
To clear the
var/view_preprocessed
, enter the following command:1
rm -r var/view_preprocessed/*
-
Several commands support an optional parameter
--clear-static-content
, which cleans generated static view files:
Clean static files
Besides the cached files, in theme development process developers also deal with other saved files - static view files that are preprocessed and published to the var/view_preprocessed
and pub/static
directories correspondingly. In most cases when working on a custom theme, for example, if you are only working on styles, you do not need to clean cache, but need to clean the previously preprocessed and published static view files. To clean them, run grunt clean <theme>
or manually clear the pub/static
and var/view_preprocessed
directories.