Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Advanced asset caching [AAC]

Tristan Lins edited this page Jan 18, 2015 · 1 revision

Since version 5 Theme+ comes with an advanced asset caching [AAC] mechanism.

In prior versions Theme+ only cache the generated asset file and do not regenerate the asset if not necessary. But this implies to fetch all assets (from the database) for a page, evaluate the filters and check the asset files. This cache also contains pre-rendered relations between pages and assets, including the filter conditions.

In pseudo logic, the cached information look like this:

if ( isMobile() ) {
    return '<script src="assets/js/mobile.js"></script>
<script src="assets/js/script.js"></script>';
}
if ( not isMobile() ) {
    return '<script src="assets/js/script.js"></script>';
}
return null;

The cache depend on pre-compiled assets, stored in the corresponding assets directory. If the asset get deleted and the AAC is used, Theme+ is unable to detect that the asset is missing. This behavior is very similar to the website caching from Contao.

FAQ

Why is the AAC important to me?

The AAC is important, if you use the asset filter. The AAC pre-compile all combinations of assets, depending on the filter settings. These informations can be used in combination with the Contao page cache. Without AAC it is impossible to combine asset filters with Contaos page caching!

It is possible to cache assets from protected pages?

Yes it is, in the maintenance operation you can select a frontend user. This frontend user must have access to all protected pages! But be careful, only protected pages will be cached with the selected frontend user, all other pages will be processed as guests.

It is possible to disable the AAC ability?

Yes it is, the AAC ability can be disabled in the system settings. This will disable the page-asset-caching, not the file caching. In other words, disable the AAC will force Theme+ to work like version 4 or prior.

Facts

  • Theme+ disable the weekly clearing of the script cache.

Known limitations

  • Similar to the website cache from Contao, the assets cache will not work with dynamically added assets, based on the request data.
  • The maintenance operation will break if a page containing an "Automatic logout" module. Disable the caching for such pages in the page expert settings.

Under the hood

A cache entry look like this (you can see this while using the "Analyse cache" maintenance feature):

/**
 * Assets cache
 *
 * created at: Tue, 06 Jan 2015 10:47:11 +0100
 * page id: 48
 * layout id: 17
 */

if ($mobileDetect->isMobile()) {
    /*
     * Debug information:
     *
     * collection(Assetic\Asset\AssetCollection) {
     *   target path: assets/js/c86f07b6-collection.js
     *   last modified: 1417550415
     *   elements: [
     *     delegator(Bit3\Contao\ThemePlus\Asset\DatabaseAsset) {
     *       type: js
     *       delegate: [
     *         asset(Assetic\Asset\FileAsset) {
     *           source path: files/theme/mobile.js
     *           source root: /var/www/contao
     *           target path: assets/js/c86f07b6-collection_script_1.js
     *           last modified: 1417550415
     *         }
     *       ]
     *     },
     *     delegator(Bit3\Contao\ThemePlus\Asset\DatabaseAsset) {
     *       type: js
     *       delegate: [
     *         asset(Assetic\Asset\FileAsset) {
     *           source path: files/theme/script.js
     *           source root: /var/www/contao
     *           target path: assets/js/c86f07b6-collection_script_2.js
     *           last modified: 1417550415
     *         }
     *       ]
     *     }
     * }
     */
    return '<script src="assets/js/c86f07b6-collection.js"></script>
';
}

if (true) {
    /*
     * Debug information:
     *
     * collection(Assetic\Asset\AssetCollection) {
     *   target path: assets/js/d2c3a3fd-collection.js
     *   last modified: 1417550415
     *   elements: [
     *     delegator(Bit3\Contao\ThemePlus\Asset\DatabaseAsset) {
     *       type: js
     *       delegate: [
     *         asset(Assetic\Asset\FileAsset) {
     *           source path: files/theme/script.js
     *           source root: /var/www/contao
     *           target path: assets/js/d2c3a3fd-collection_script_1.js
     *           last modified: 1417550415
     *         }
     *       ]
     *     }
     * }
     */
    return '<script src="assets/js/d2c3a3fd-collection.js"></script>
';
}

return null;

The example contains 2 scripts, one for mobile including mobile.js and script.js and one for non-mobile clients only including script.js. In the Contao backend, there is a mobile platform filter enabled for the mobile.js file.

And yes, this IS php code. The complete logic will be pre-compiled and written as php code into the cache. These generated php code will be evaluated on the request. This is still much faster, then fetch all assets from the database and evaluate them.