Wrap a chain of transformers with a PSR-6 cache layer. A cache key is built from the input value; if the cache contains an item for this key it is returned directly, otherwise the wrapped transformers are applied and the result is stored in cache.
- Service:
CleverAge\ProcessBundle\Transformer\CachedTransformer - Transformer code:
cached
any: the value is used both to build the cache key (after key_transformers) and as input of transformers.
The key value must be a string once key_transformers are applied, otherwise the cache is bypassed (see Notes).
any: result of the transformers chain, or the cached value on cache hit.
| Code | Type | Required | Default | Description |
|---|---|---|---|---|
cache_key |
string |
X | Root of the cache key. The final key is <cache_key>|<rawurlencode(key value)> |
|
ttl |
string|DateTimeInterface|null |
null |
Expiration date of the cache items. A string is converted with new \DateTime($value) (e.g. +1 hour). null means no expiry |
|
transformers |
array |
[] |
Transformers applied on cache miss, see TransformerTrait | |
key_transformers |
array |
[] |
Transformers applied on the input to compute the key value, see TransformerTrait |
- Cache a costly transformation for one hour, using a slugified input as key
# Transformer options level
cached:
cache_key: my_prefix
ttl: '+1 hour'
key_transformers:
slugify: ~
transformers:
callback:
callback: md5- The cache pool is the
cache.appservice, passed explicitly. To use another pool, override the$cacheargument of theCleverAge\ProcessBundle\Transformer\CachedTransformerservice in your application (arguments: { $cache: '@cache.my_pool' }). Items are saved withsaveDeferred(), a warning is logged if the save fails. - A string
ttlis converted to a date when the options are resolved (i.e. once, when the transformer is configured), not each time an item is saved: all items share the same absolute expiration date. - If the key value is not a string after
key_transformers(e.g. anintinput without acastkey transformer), or if the cache pool raises a PSR-6InvalidArgumentException(logged as a warning), the transformers are applied without cache.