symfony/symfony — pull request #11452 by giosh94mhz (2014-07-23 13:47:31+00:00)
Title: [DoctrineBridge][DI] use a service for cache instance

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |

## What?
I've improved the way the memcache/redis cache driver for Doctrine can be configured.

## Why?
In the current implementation, to create (e.g.) a redis connection one have to define under doctrine ORM something like this:
```yaml
doctrine:
orm:
entity_managers:
default:
query_cache_driver:
type: redis
host: cache-server.example.com
port: 12345
# instance_class: Redis # default value
# class: Doctrine\Common\Cache\RedisCache
````
This way, the DI component, allocate a `new Redis` object, start a new connection, and bind it to the CacheDriver with a (carefully generated) namespace.

The complication starts if you use Redis for multiple services and in many different context. To reuse the same connection, you have to create a service and use it in multiple places:
```xml

cache-server.example.com
12345

```
Than you do:
```yml
doctrine:
orm:
entity_managers:
default:
query_cache_driver:
type: service
id: acme.redis
```
But this introduce another problem: the namespace is not set (or set only once) and than, the service ``acme.redis`` cannot be used in multiple content unless you are absolutely sure that no cache collision happens. In my experience collisions always happens, since there are multiple instance/version of the same project on a staging/production server.

## How?
To solve the issue, it is enough to provide a way to set the connection instance to the cache driver. I've modified the DI so that the following can be used for multiple configuration:
```xml

cache-server.example.com
12345

```
```yaml
doctrine:
orm:
entity_managers:
default:
query_cache_driver:
type: redis
instance_id: acme.connection
# another em just to show the usage
another:
query_cache_driver:
type: redis
instance_id: acme.connection

```
Now, the connection is created once and namespaces are no longer an issue, since a cache profile is created for each context.

Todo:
I've used the new name `instance_id` to be consistent with `instance_class`, but this require a new configuration entry. Maybe, we can just reuse the `id` entry.

Also, It would be nice if someone would point me to the right place for the new documentation (docs? cookbook?).

Checklist:
- [ ] gather feedback for my changes
- [ ] submit changes to the doctrine bundle (required only if not using the `id` entry)
- [ ] submit changes to the documentation