When developing Magento websites, performance begins to lag, with the slower page loading as more resources and more content is added to the site. The use of cache technology can mitigate speed losses and in certain instances, can even speed up page loads.
This article will explain cache /caching types in Magento in more detail.
Caching is one of the best ways to improve a website’s performance.
In the client-server model used by most web applications, there are two ways to cache content:
- From the client-side (I.E. the web browser )
Magento 2 Cache & Caching Types
- The page cache library contains a reverse PHP proxythat enables full page caching, which is available as an out-of-the-box enhancement in Magento 2. A reverse proxy acts as a middle-man between visitors and the application in order to reduce server page load time.
Cacheable and un-Cacheable pages
- These are terms that are used to indicate whether or not a page should be cached or not.
- Pages are cacheable by default.
- The entire page is uncacheable if the layout or block is uncacheable,.
To create an uncacheable page mark any block on that page as
uncacheable in the layout using cacheable="false"
Public & Private content
- Reverse proxies serve “public” or shared content to more than one user.
- Some Magento websites will generate dynamic and personalized “private” content which should only be served to a single user.
Magento can distinguish between two types of content
- This content is stored server-side in reverse proxy cache storage.
- The content is stored client-side and is specific to an individual customer.
- Shopping Cart
- Customer’s Name and Address
Cache types + Cache Type Codes
- Configuration - config
- Magento collects configuration from all the modules, it then merges it, and saves the merged results to the cache.
- It contains the store-specific settings stored in the file system and database.
- After modifying the configuration you should clean or flush this cache type
- Layout - layout
- Compiled page layouts.
- 'clean' or 'flush' this cache type after modifying any 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.
- Magento cleans up this cache automatically, 3rd-party developers can put any data in any segment of the cache.
- Clean or flush this cache type after modifying the code level that affects HTML output.
- Translations - translate
- Merged translations from all modules.
- Collections data - collections
- Database Queries results.
- When it is necessary Magento cleans up the cache automatically, third-party developers can put any data in any segment of the cache.
- DDL - db_ddl
- Schema for database
- When necessary, Magento will clean up this cache automatically, third-party developers can put any data in any segment of the cache.
- After you make custom changes to the database schema you should 'clean' or 'flush' this cache type.
- One way to update the database schema automatically:
- Compiled Config - compiled_config
- Compilation configuration
- Entity attribute value (EAV) - eav
- Metadata to EAV attributes.
- You should not need to 'clean' or 'flush' this cache type.
- Reflection - reflection
- Dependency between the Webapi module and the Customer module is removed.
- Integration configuration - config_integration
- You need to 'clean' or 'flush' cache after changing or adding compiled integrations.
- Integration API configuration - config_integration_api
- Integration APIs configuration of the Store’s Integrations compiled.
- Web services configuration - config_webservice
- Caching the Web API Structure.
- Customer Notification - customer_notification
- Notifications appear in the user interface temporarily.
Viewing Cache Status using SSH/FTP
To view status of cache, enter:
- $ bin/magento cache:status
A sample follows:
- 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
Enable or disable cache types using SSH/FTP
To enable/disable cache, enter:
- $ bin/magento cache:enable [type]
- $ bin/magento cache:disable [type]
*Disabled cache types are not cleaned.
Difference between cache clean and cache flush:
Magento 2 Clean Cache:
- Deletes all enabled cache types from Magento 2, to which the disabled cache types are not cleaned.
Magento 2 Flush Cache:
- Cleans all cache types in storage. It might affect processing applications that run the same cache storage.
Clean Cache using SSH/FTP
To ‘clean’ cache, run:
- $ bin/Magento cache:clean
Cache type cleaning deletes all items from enabled Magento cache types only. This option does not affect processes or applications, it only cleans the cache that Magento uses.
Flush Cache using SSH/FTP
To ‘flush’ cache, run:
- $ bin/magento cache:flush
Refreshing Cache using Cache Management
In the Magento Admin
- Go to System> Tools > Cache Management
- Select one or all ‘Cache Type’you want to Refresh
- Then click on Submit
Magento 2 Clear Cache Programmatically:
Store admin may have requirements to clean and flush cache programmatically with-in Magento 2. It's advisable to implement in order to offer speedy store to store users.