Performance Optimization & Caching
Relevant source files
The following files were used as context for generating this wiki page:
Performance Optimization and Caching in PrestaShop involves multiple layers of data persistence and delivery acceleration. The system manages server-side templates, directory-level access optimizations, and external caching systems to reduce latency and server load. These optimizations include Smarty template compilation, .htaccess file generation for Apache optimization, and integration with distributed memory object caching systems like Memcached.
The core logic resides in utility classes that manage cache clearing, static file headers, and server-side environment configurations. Developers and administrators control these features through the Back Office performance parameters, which toggle specific optimization drivers and clear accumulated cache data.
Server-Side Caching Systems
PrestaShop supports several caching mechanisms to accelerate request processing. These systems range from internal PHP object caching to external distributed memory systems.
Smarty Cache Management
Smarty caching reduces the time spent rendering templates by storing compiled versions of TPL files. The system allows forcing compilation for development or enabling cache for production environments. The Tools class provides methods to enable, restore, and clear these settings.
The diagram above illustrates the logic flow when enabling Smarty cache through core utility methods.
Tools::enableCache($level, $context): Configures Smarty to use a specific caching level and sets a long-term cache lifetime. Sources:[classes/Tools.php:1232-1249](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1232-L1249)Tools::clearSmartyCache(): Executes bothclearCache()andclearCompile()to refresh template data. Sources:[classes/Tools.php:1635-1640](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1635-L1640)
External Caching Drivers
The Back Office interface allows users to select and configure external caching systems. Supported systems include CacheMemcache and CacheMemcached. The UI dynamically toggles server list blocks based on the selected driver.
| Caching System | Description |
|---|---|
| CacheMemcache | Integration with the Memcache PHP extension. |
| CacheMemcached | Integration with the Memcached PHP extension. |
| Smarty Cache | File-based caching for template rendering. |
Sources: [admin-dev/themes/default/js/bundle/admin_parameters/performancePageUI.js:31-50](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/admin-dev/themes/default/js/bundle/admin_parameters/performancePageUI.js#L31-L50), [classes/Tools.php:1235](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1235)
Apache Optimization and .htaccess Generation
PrestaShop optimizes Apache web server performance by generating specific rules within the .htaccess file. These rules handle directory indexing, GZIP compression, and cache headers for static assets.
Static File Cache Control
When enabled, the system injects mod_expires and mod_headers directives into the .htaccess file. This instructs browsers to cache static resources for specific durations, reducing repeated requests for images, CSS, and JavaScript.
- Image Cache:
access plus 1 month - CSS/JS Cache:
access plus 1 week - Icon/SVG Cache:
access plus 1 year - Fonts:
access plus 1 year
Sources: [classes/Tools.php:1082-1110](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1082-L1110)
Compression and Header Management
The system enables GZIP compression via mod_deflate for text-based formats and fonts. It also unsets the Etag header to prevent specific caching issues in clustered environments. Sources: [classes/Tools.php:1112-1123](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1112-L1123)
This flow shows how performance-related directives are conditionally added to the server configuration.
Cache Clearing and Maintenance
Maintaining performance requires the ability to purge stale data across different application layers. The Tools class acts as a central hub for clearing these caches.
Unified Cache Purge
The system provides a clearAllCache() method that purges both legacy Smarty files and modern Symfony container/cache data.
- Smarty Cache: Deletes compiled templates and cached HTML.
- Symfony Cache: Utilizes the
symfony_cache_clearerservice or removes the/var/cache/{env}/directory. - Hook Execution: Dispatches
actionClearCache,actionClearCompileCache, andactionClearSf2Cacheto allow modules to purge their own data.
Sources: [classes/Tools.php:1642-1681](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1642-L1681)
File and XML Caching
Specific data structures, such as CLDR (Common Locale Data Repository) data and XML configuration files, are cached locally to avoid redundant parsing.
Tools::clearXMLCache(): Scans theconfig/xmldirectory and deletes non-default XML files. Sources:[classes/Tools.php:671-679](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L671-L679)Tools::$file_exists_cache: A static array that stores the results offile_exists()calls to speed up recurring filesystem checks. Sources:[classes/Tools.php:159-165](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L159-L165)
UI Performance and Pagination
Performance in the Back Office is managed through efficient data loading in grids and list views.
Pagination Limits
Grid performance is maintained by limiting the number of elements displayed per page. The default pagination options (e.g., 10, 20, 50, 100 items) reduce the memory footprint of individual Back Office requests.
Sources: [tests/UI/campaigns/functional/BO/08_design/04_pages/pages/03_pagination.ts:98-125](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/tests/UI/campaigns/functional/BO/08_design/04_pages/pages/03_pagination.ts#L98-L125)
Dynamic UI Updates
The performance page uses JavaScript to toggle relevant configuration fields. This prevents the browser from rendering unnecessary form elements for inactive caching drivers or debug modes. Sources: [admin-dev/themes/default/js/bundle/admin_parameters/performancePageUI.js:9-29](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/admin-dev/themes/default/js/bundle/admin_parameters/performancePageUI.js#L9-L29)
System Constants and Limits
The system defines specific constants and methods to handle memory and execution limits.
Tools::getMemoryLimit(): Retrieves the PHPmemory_limitand converts it into octets for comparison. Sources:[classes/Tools.php:1687-1692](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L1687-L1692)Tools::CACHE_LIFETIME_SECONDS: Defines a default cache lifetime of 604,800 seconds (1 week). Sources:[classes/Tools.php:26](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L26)Tools::ps_round(): Provides standardized rounding for prices, respecting thePS_PRICE_ROUND_MODEconfiguration to ensure consistent performance in financial calculations. Sources:[classes/Tools.php:885-915](https://github.com/mattgoud-sandbox/PrestaShop/blob/a9e8f98a90dc6c32ecb7a82c567c16f0a892c444/classes/Tools.php#L885-L915)
Performance Optimization & Caching in PrestaShop ensures the application scales by offloading processing to specialized drivers and minimizing filesystem and database hits. Through the management of .htaccess headers, template persistence, and unified cache purges, the system maintains a balance between data freshness and execution speed.