8000 GitHub - mage2sk/module-cachemanager: Smart cache invalidation on entity save and automated cache warmup with concurrent requests for Magento 2. · GitHub
Skip to content

Repository files navigation

Magento 2 Cache Manager

Panth Cache Manager adds two things to a Magento 2 store: an observer that cleans the cache tags of a product, category, CMS page or CMS block when it is saved, and a cache warmup that requests the store's home, category, product and CMS page URLs over HTTP so that the full page cache is populated before real visitors arrive. Warmup runs from a Magento cron job on a configurable schedule (every six hours by default) or on demand from a console command, and every request is recorded in a database table that is shown as a grid in the admin.

The module works at the cache and HTTP layer and ships no frontend templates or web assets, so it does not depend on the theme in use. It is intended for store administrators who want scheduled warmup with a request log, and for developers who want to run warmup from the command line.

Product page: Magento 2 Cache Manager

Features

  • Cleans the cache tags of the saved product, category, CMS page or CMS block when that entity is saved, with separate on/off switches for products, categories and CMS content.
  • Cron job panth_cachemanager_warmup that requests the selected page types on the configured schedule using curl_multi, with a configurable number of parallel requests (default 5).
  • Page types available for warmup: "Home Page", "Category Pages", "Product Pages" and "CMS Pages".
  • Console command panth:cachemanager:warmup that runs the same warmup synchronously and prints per-URL results and a summary.
  • Database table panth_cache_warmup_log that stores URL, page type, HTTP status, result, response time and timestamp for every warmup request.
  • Admin grid "Warmup Log" under "Panth Extensions" with filters, sorting, column controls and bookmarks.
  • All settings except "Warmup Schedule (Cron)" can be set at default, website and store view scope; the schedule is set at default scope only.
  • Unit tests for the helper, cron and observer classes under Test/Unit.

Compatibility

Platform Versions
Magento Open Source 2.4.4 to 2.4.8
Adobe Commerce 2.4.4 to 2.4.8
PHP 8.1 or later (php: >=8.1 in composer.json)

Composer constraints on Magento packages: magento/framework ^103.0 or ^104.0, magento/module-store ^101.0 or ^102.0, magento/module-catalog ^104.0 or ^105.0, magento/module-cms ^104.0 or ^105.0.

Requirements

  • Magento Open Source or Adobe Commerce 2.4.4 to 2.4.8.
  • PHP 8.1 or later with the curl extension (ext-curl is a Composer requirement; warmup uses the curl_multi functions).
  • mage2kishan/module-core ^1.0 (module Panth_Core). The configuration helper extends Panth\Core\Helper\AbstractConfig, and the admin menu, ACL and configuration tab hang off the "Panth Extensions" entries that Panth_Core provides. Composer installs it automatically.
  • The module sequence also declares Magento_Backend and Magento_PageCache.
  • Magento cron must be running for scheduled warmup. Warmup requests are plain HTTP GET requests to the store's base URL, so the store must be reachable from the server that runs cron. The module does not talk to Varnish or Redis directly.

Installation

composer require mage2kishan/module-cachemanager
bin/magento module:enable Panth_Core Panth_CacheManager
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush

setup:di:compile is only needed in production mode. The module ships no files under view/*/web, so setup:static-content:deploy is not required for it.

Check that the module is enabled:

bin/magento module:status Panth_CacheManager

After installation the module is switched off ("Enable Cache Manager" defaults to No), so nothing changes until it is enabled in the configuration.

Configuration

Go to Stores > Configuration > Panth Extensions > Cache Manager. Every field except "Warmup Schedule (Cron)" can be set at default, website and store view scope. All config paths start with panth_cachemanager/.

General Settings

Setting Default What it does
Enable Cache Manager No Master switch. When set to No, the save observer does nothing and the warmup cron job does nothing. Path: panth_cachemanager/general/enabled.

Full Page Cache

Shown only when "Enable Cache Manager" is Yes.

Setting Default What it does
Cache TTL (seconds) 86400 Stored at panth_cachemanager/full_page/ttl and exposed through Helper\Data::getCacheTtl(). No code in this module applies the value to the page cache, so changing it has no effect on its own.

Cache Warmup

Shown only when "Enable Cache Manager" is Yes; the last three fields are shown only when "Enable Cache Warmup" is Yes.

Setting Default What it does
Enable Cache Warmup No Allows the cron job and the console command to run. Path: panth_cachemanager/warmup/enabled.
Warmup Schedule (Cron) 0 */6 * * * Cron expression for the panth_cachemanager_warmup job, read through the config_path of the job in etc/crontab.xml. Default scope only. If it is empty, 0 */6 * * * is used. Path: panth_cachemanager/warmup/warmup_schedule.
Pages to Warm Up Home Page, Category Pages, Product Pages, CMS Pages Multiselect of page types to request. Path: panth_cachemanager/warmup/warmup_pages, stored as home,catalog_category,catalog_product,cms.
Concurrent Requests 5 Number of URLs requested in parallel per batch. Must be greater than zero. Path: panth_cachemanager/warmup/concurrent_requests.

Cache Invalidation

Shown only when "Enable Cache Manager" is Yes; the three per-entity fields are shown only when "Enable Smart Invalidation" is Yes.

Setting Default What it does
Enable Smart Invalidation Yes Enables the save observer. Path: panth_cachemanager/invalidation/smart_invalidation.
Invalidate on Product Save Yes On catalog_product_save_after, cleans the cache tags of the saved product. Path: panth_cachemanager/invalidation/invalidate_on_product_save.
Invalidate on Category Save Yes On catalog_category_save_after, cleans the cache tags of the saved category. Path: panth_cachemanager/invalidation/invalidate_on_category_save.
Invalidate on CMS Save Yes On cms_page_save_after and cms_block_save_after, cleans the cache tags of the saved page or block. Path: panth_cachemanager/invalidation/invalidate_on_cms_save.

Admin menu

The admin sidebar gets a "Cache Manager" entry under "Panth Extensions" with two items: "Warmup Log", which opens the request log grid, and "Configuration", which opens the configuration section above. Both require the ACL resource Panth_CacheManager::config.

Usage

Smart invalidation

When the module and "Enable Smart Invalidation" are both on, saving a product, category, CMS page or CMS block runs Observer\CacheInvalidate. The observer checks the per-entity switch and then calls Magento\Framework\App\CacheInterface::clean() with the tags returned by the saved entity's getIdentities() (for example cat_p_42 for product 42), so only entries belonging to that entity are removed. If the event carries no entity or the entity returns no identities, it falls back to the tag for the entity type (catalog_product, catalog_category, cms_page or cms_block). Each invalidation is written to the Magento log at info level with the event name and tags. Errors are logged at error level and do not interrupt the save.

Scheduled warmup

Cron job panth_cachemanager_warmup (cron group default, schedule from "Warmup Schedule (Cron)", default 0 */6 * * *) runs Cron\WarmupCache::execute(). It returns immediately unless both "Enable Cache Manager" and "Enable Cache Warmup" are Yes. Otherwise it:

  1. Builds the URL list from the selected page types, using the base URL of the store that cron runs under: the home page (/), active categories with level greater than 1 (<base_url>/<url_path><category_url_suffix>), enabled products in that store's website with visibility "Catalog" or "Catalog, Search" (<base_url>/<url_key><product_url_suffix>), and active CMS pages of that store except no-route (<base_url>/<identifier>). The suffixes come from catalog/seo/category_url_suffix and catalog/seo/product_url_suffix. Duplicate URLs are removed, and only http or https URLs on the host and port of the store base URL are requested.
  2. Splits the list into batches of "Concurrent Requests" URLs and requests each batch with curl_multi. Each request is limited to http and https, does not follow redirects (a 3xx response is recorded as success), verifies the SSL certificate, uses a 10 second connect timeout and a 30 second total timeout, and sends the user agent PanthCacheManager/1.0 (Warmup).
  3. Records every request in panth_cache_warmup_log with status success (no curl error and HTTP status below 400) or failed.

Product URLs are built from url_key and do not include category paths. Any exception during the run is logged as CacheManager Cron Error.

Console command

bin/magento panth:cachemanager:warmup
bin/magento panth:cachemanager:warmup --quiet-rows

Runs the same warmup synchronously and writes to the log table. If "Enable Cache Manager" or "Enable Cache Warmup" is No, the command prints a notice and exits with 0 without requesting anything. Without options it prints one line per URL (OK or FAIL, HTTP status, response time, URL) as results arrive, followed by a summary line with success and failure counts, average response time and total wall time. With --quiet-rows the per-URL lines are not printed and only the summary line is shown. The exit code is 0 when every request succeeded and 1 when at least one failed. If no URLs were collected the command prints a notice and exits with 0.

Warmup Log grid

"Panth Extensions" > "Cache Manager" > "Warmup Log" (admin route panth_cachemanager/warmup/index, page title "Cache Warmup Log") lists every warmup request with the columns ID, URL, Page Type, HTTP Status, Status, Response Time (ms) and Warmed At. All columns can be filtered and sorted. Page Type is the page type the URL was collected for: home, category, product or cms. Nothing prunes this table; it grows with every warmup run.

Developer Notes

  • Module name: Panth_CacheManager. Composer package: mage2kishan/module-cachemanager. PHP namespace: Panth\CacheManager.
  • Helper\Data (extends Panth\Core\Helper\AbstractConfig): isEnabled(), getCacheTtl(), isWarmupEnabled(), getWarmupPages(), getWarmupSchedule(), getConcurrentRequests(), isSmartInvalidationEnabled(), shouldInvalidateOnProductSave(), shouldInvalidateOnCategorySave(), shouldInvalidateOnCmsSave(), getProductUrlSuffix(), getCategoryUrlSuffix(). All accept an optional store ID.
  • Cron\WarmupCache: execute() for cron and runWarmup(?callable $onResult = null): array, which returns one row per URL (url, page_type, status, http_code, response_time_ms, error) and calls the optional callback with each row as it completes. This is the entry point for running warmup from custom code.
  • Console\Command\WarmupCommand: registered in etc/di.xml on Magento\Framework\Console\CommandList as panth_cachemanager_warmup.
  • Observer\CacheInvalidate: registered in etc/events.xml for catalog_product_save_after, catalog_category_save_after, cms_page_save_after and cms_block_save_after.
  • Controller\Adminhtml\Warmup\Index: admin controller for the grid (ADMIN_RESOURCE = Panth_CacheManager::config); layout panth_cachemanager_warmup_index.xml; UI component panth_cachemanager_warmup_listing backed by Model\ResourceModel\WarmupLog\Grid\Collection (data source panth_cachemanager_warmup_listing_data_source).
  • Model\WarmupLog, Model\ResourceModel\WarmupLog and Model\ResourceModel\WarmupLog\Collection: entity, resource model and collection for the log table.
  • Model\Config\Source\WarmupPages: option source with the values home, catalog_category, catalog_product, cms. Model\Config\Backend\Enabled: backend model for the master switch (currently only calls the parent beforeSave()).
  • ACL resources: Panth_CacheManager::cachemanager ("Cache Manager") and Panth_CacheManager::config ("Configuration"), both under Panth_Core::panth_extensions.
  • Admin route: panth_cachemanager (front name panth_cachemanager). There are no frontend routes, plugins, preferences, web API endpoints or frontend view files.
  • Database table panth_cache_warmup_log (from etc/db_schema.xml): log_id (primary key), url, page_type, http_status, status (default pending), response_time (decimal, milliseconds), warmed_at (timestamp, default current time). Indexes on status and warmed_at.

Uninstallation

bin/magento module:disable Panth_CacheManager
composer remove mage2kishan/module-cachemanager
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush

The table panth_cache_warmup_log and the configuration values stored under panth_cachemanager/ in core_config_data remain in the database after these commands; drop or delete them manually if they are no longer wanted.

Support

Documentation

USER_GUIDE.md covers installation, each configuration group (General Settings, Full Page Cache, Cache Warmup, Cache Invalidation), the Warmup Log grid, a troubleshooting table and support contacts.

License

Commercial software license. See LICENSE.txt in this repository.

Changelog

See CHANGELOG.md.

Links

About

Smart cache invalidation on entity save and automated cache warmup with concurrent requests for Magento 2.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0