Cache Resolvers

2.0 version
Maintained

Cache Resolvers

A number of built-in cache resolvers are available:

Set the Default Cache Resolver

The default cache is the web path cache resolver, which caches images under /media/cache/ within your application web root path.

You can specify the cache resolver to use per individual filter_sets or globally. To set the default cache resolver globally, use:

1
2
3
4
# app/config/config.yml

liip_imagine:
    cache: your_resolver

To change the default configuration, you can redefine the default cache resolver by explicitly defining a resolver called default:

1
2
3
4
5
6
7
# app/config/config.yml

liip_imagine:
    resolvers:
        default:
            web_path:
                cache_prefix: custom_path

To change the cache resolver for a specific filter_set, use the following configuration.

1
2
3
4
5
6
7
8
9
# app/config/config.yml

liip_imagine:
    filter_sets:
        cache: ~
        my_thumb:
            cache: your_resolver
            filters:
                # the filter list

Custom Cache Resolver

You can easily define your own, custom cache resolvers to handle cache resolution using any imaginable backend. Creating a custom cache resolver begins by creating a class that implements the ResolverInterface, as shown below.

1
2
3
4
5
6
7
interface ResolverInterface
{
    public function isStored($path, $filter);
    public function resolve($path, $filter);
    public function store(BinaryInterface $binary, $path, $filter);
    public function remove(array $paths, array $filters);
}

The following is a template for creating your own cache resolver. You must provide implementations for all methods to create a valid cache resolver.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace AppBundle\Imagine\Cache\Resolver;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;

class MyCustomResolver implements ResolverInterface
{
    /**
     * @param string $path
     * @param string $filter
     *
     * @return bool
     */
    public function isStored($path, $filter)
    {
        /** @todo: implement */
    }

    /**
     * @param string $path
     * @param string $filter
     *
     * @return string
     */
    public function resolve($path, $filter)
    {
        /** @todo: implement */
    }

    /**
     * @param BinaryInterface $binary
     * @param string          $path
     * @param string          $filter
     */
    public function store(BinaryInterface $binary, $path, $filter)
    {
        /** @todo: implement */
    }

    /**
     * @param string[] $paths
     * @param string[] $filters
     */
    public function remove(array $paths, array $filters)
    {
        /** @todo: implement */
    }
}

Once you have defined your custom cache resolver, you must define it as a service and tag it with liip_imagine.cache.resolver.

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    # app/config/services.yml
    
    services:
        imagine.cache.resolver.my_custom:
            class: AppBundle\Imagine\Cache\Resolver\MyCustomResolver
            arguments:
                - "@filesystem"
                - "@router"
            tags:
                - { name: "liip_imagine.cache.resolver", resolver: my_custom_cache }
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    <!-- app/config/services.xml -->
    
    <service id="imagine.cache.resolver.my_custom" class="AppBundle\Imagine\Cache\Resolver\MyCustomResolver">
        <tag name="liip_imagine.cache.resolver" resolver="my_custom_cache" />
        <argument type="service" id="filesystem" />
        <argument type="service" id="router" />
    </service>
    

Note

For more information on the Service Container, reference the official Symfony Service Container documentation.

Now your custom cache resolver can be set as the global default using the name defined in the resolver attribute of the tags key.

1
2
liip_imagine:
    cache: my_custom_cache

Alternatively you can only set the custom cache resolver for just a specific filter set:

1
2
3
4
liip_imagine:
    filter_sets:
        my_special_style:
            cache: my_custom_cache

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.