Skip to content
RWOverdijk edited this page Sep 21, 2012 · 13 revisions

Caching is a very important part of this module. Without caching, you're going to have a bad time. Filtering assets, looking them up and then optionally creating a collection can be resource intensive. Without caching, your response times will go down and with mild popularity so will your webserver.

This page assumes that you've taken a look at the quick start, and that you're already serving a basic asset.


Caching types

This module is largely based on assetic. That being said, it's possible to use any of the default caching methods supplied by assetic.

AssetManager also suplies another caching method to store files the way you retrieved them. I will be covering all caching types.

Caching assets

Specifying to which assets the caching types apply is done with the key for the array entry. There's a default key, which is the default and there's an option to add specific assets, being the exception.

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'default' => array(
                'cache'     => 'CachingType',  // Apc, FilePath, FileSystem etc.
            ),
            'css/exception.css' => array(
                'cache'     => 'CachingType',  // Apc, FilePath, FileSystem etc.
            ),
        ),
    ),
);

APC

APC is a well known opcode cache. I will therefor not explain in detail what it does. If you do wish to find out what it does, I recommend taking a look at the documentation.

To enable the APC cache, simply add the following to your config:

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'default' => array(
                'cache'     => 'Apc',
            ),
        ),
    ),
);

And that's it! the first request will perform all required actions on your assets, and the second request will serve it from APC. The default key specifies that this should be applied to all assets. If you wanted to specify this for a very specific asset, or asset collection, you'd do this:

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'test-asset.css' => array(
                'cache'     => 'Apc',
            ),
        ),
    ),
);

By simply changing the key. You could also combine them, and cache everything through APC and a specific asset through something else:

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'default' => array(
                'cache'     => 'Apc',
            ),
            'test-asset.css' => array(
                'cache'     => 'Filesystem',
                'options' => array(
                    'dir' => 'cache'
                ),
            ),
        ),
    ),
);

FileSystem

The filesystem cache essentially takes the file, creates a "cache hash" and then stores the file in a directory specified by you. Nothing special about it, and the example can be seen here:

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'test-asset.css' => array(
                'cache'     => 'Filesystem',
                'options' => array(
                    'dir' => 'cache'
                ),
            ),
        ),
    ),
);

FilePath

The filepath cache is essentially the same as the FileSystem cache. It just has one big difference, being the storage location. FileSystem cache caches the files using hashes, where FilePath cache simply takes the entire path and re-creates it. This can come in handy (and is the one I use most) because it then serves the file directly without going through php.

Example:

<?php
return array(
    'asset_manager' => array(
        'caching' => array(
            'test-asset.css' => array(
                'cache'     => 'FilePath',
                'options' => array(
                    'dir' => 'cache'
                ),
            ),
        ),
    ),
);

Notes

  • It is possible to set the cache type in three ways. First is FilePath, second is FilePathCache and the third is any instance of Assetic\Asset\Cache, which allows you to create your custom cachers.

Clone this wiki locally