-
Notifications
You must be signed in to change notification settings - Fork 82
Caching
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.
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.
),
),
),
);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.
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' => 'public', // path/to/cache
),
),
),
),
);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' => 'public', // path/to/cache
),
),
),
),
);The FilePath cache is essentially the same as the Filesystem cache. The main difference is that the Filesystem cache adapter serves the cache files using their hashes (check for cache existence and track modification date) thus every time going through PHP, whereas the FilePath just copies the original files (after applying the filters and combining the collections) to the specified directory just once. This allows you to serve the file statically. So the behavior of Filesystem is most suitable for development purposes while FilePath would be a better fit for production.
Example:
<?php
return array(
'asset_manager' => array(
'caching' => array(
'test-asset.css' => array(
'cache' => 'FilePath',
'options' => array(
'dir' => 'public', // path/to/cache
),
),
),
),
);- It is possible to set the
cachetype in three ways. First isFilePath, second isFilePathCacheand the third is any instance of Assetic\Asset\Cache, which allows you to create your custom cachers.