Skip to content
Westin Shafer edited this page May 6, 2014 · 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 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.
            ),
        ),
    ),
);
## Caching types This module is largely based on [assetic](https://github.com/kriswallsmith/assetic). That being said, it's possible to use any of the [default caching methods supplied by assetic.](https://github.com/kriswallsmith/assetic/tree/master/src/Assetic/Cache)

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

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'     => '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
                ),
            ),
        ),
    ),
);
### 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'     => 'Assetic\\Cache\\FilesystemCache',
                'options' => array(
                    'dir' => 'public', // path/to/cache
                ),
            ),
        ),
    ),
);
### FilePath 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'     => 'AssetManager\\Cache\\FilePathCache',
                'options' => array(
                    'dir' => 'public', // path/to/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. ### ZendCacheAdapter The Zend Cache Adapter wraps up a configured Zend Cache instance for use with Asset Manager. This allows you to use any cache provider currently supplied by Zend Cache. To use the Zend Cache Adapter you will need to wrap your currently configured Zend Cache through the provided wrapper and define this as a ZF2 service.

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', 
            ),
        ),
    ),
);
### Custom You can also build and use your own custom strategy. Start by simply implementing the Assetic\Cache\CacheInterface in your cache provider then define that service (or invokable) in the Asset Managers configuration.

[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', 
            ),
        ),
    ),
);
Clone this wiki locally