-
Notifications
You must be signed in to change notification settings - Fork 83
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.
## 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.
),
),
),
);
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' => 'Assetic\\Cache\\ApcCache',
),
),
),
);
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' => 'Assetic\\Cache\\ApcCache',
),
),
),
);
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' => 'Assetic\\Cache\\ApcCache',
),
'test-asset.css' => array(
'cache' => 'Assetic\\Cache\\FilesystemCache',
'options' => array(
'dir' => 'public', // path/to/cache
),
),
),
),
);
<?php
return array(
'asset_manager' => array(
'caching' => array(
'test-asset.css' => array(
'cache' => 'Assetic\\Cache\\FilesystemCache',
'options' => array(
'dir' => 'public', // path/to/cache
),
),
),
),
);
Example:
<?php
return array(
'asset_manager' => array(
'caching' => array(
'test-asset.css' => array(
'cache' => 'AssetManager\\Cache\\FilePathCache',
'options' => array(
'dir' => 'public', // path/to/cache
),
),
),
),
);
To get started first you will need to have a Zend Cache Service defined. This is well documented so we won't be describing that here. You can find out more about creating a Zend Cache Service by reading the Zend Frameworks documentation.
Due to the differences between Assetic Caching and Zend's Caching you will need to wrap your Zend Cache service with our provided adapter. If we follow the documentation from Zend Framework then our first step will be to create a new Service Factory that does just that.
[MyApp]/src/Factory/AssetManagerZendCacheFactory
<?php
namespace MyApp\Factory;
use AssetManager\Cache\ZendCacheAdapter;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AssetManagerZendCacheFactory implements FactoryInterface
{
/**
* Creates Service
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
/** @var \Zend\Cache\Storage\StorageInterface $zendCache */
$zendCache = $serviceLocator->get('My\Zend\Cache\Service');
return new ZendCacheAdapter($zendCache);
}
}
Next you will need to add the appropriate configuration to your application or module. First we will be defining the service to the service manager, then you can configure the asset manager cache in the exact same way described above using your service key as the 'cache' strategy.
[MyApp]/config/module.config.php
return array(
// Define the service to the ZF2 service manager
'service_manager' => array(
'factories' => array (
'MyApp\\Cache\\AssetManagerZendCache'
=> 'MyApp\\Factory\\AssetManagerZendCacheFactory',
),
),
// Tell the Asset Manager to use this service for your cache.
'asset_manager' => array(
'caching' => array(
'default' => array(
// Your wrapped service goes here
'cache' => 'MyApp\\Cache\\AssetManagerZendCache',
),
),
),
);
[MyApp]/config/module.config.php
return array(
// Tell the Asset Manager to use your service or invokable as the cache provider.
'asset_manager' => array(
'caching' => array(
'default' => array(
// Your service or invokable goes here
'cache' => 'MyService',
),
),
),
);