Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: database option for redis cache, per driver cache namespace #72

Open
wants to merge 3 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ Configuration
String or array describing result cache implementation.
* **hydration_cache** (Default: setting specified by orm.default_cache):
String or array describing hydration cache implementation.
* **types**
* **cache_namespace**:
String to define a global namespace for cached keys.
* **types**:
An array of custom types in the format of 'typeName' => 'Namespace\To\Type\Class'
* **orm.ems.options**:
Array of Entity Manager configuration sets indexed by each Entity Manager's
Expand Down Expand Up @@ -290,6 +292,31 @@ Configuration
`Doctrine\ORM\Mapping\EntityListenerResolver`.
* **orm.default_cache**:
String or array describing default cache implementation.

Example configuration:
```php
<?php
$app['orm.ems.options'] = array(
// php array cache defined globally
'orm.default_cache' => 'array',
// or
'orm.default_cache' => array('driver' => 'array'),

// or redis cache defined as metadata cache on the default em
'default' => array(
...
'metadata_cache' => array(
'driver' => 'redis',
'host' => '127.0.0.1', // mandatory redis host
'port' => 6379, // mandatory redis port
'password' => 'mypassword', // optional redis password
'database' => 1, // optional redis database, default: 0
'namespace' => 'MYNAMESPACE:' // optional namespace for this cache, otherwise cache_namespace from the current em will be used
)
...
)
);
```
* **orm.add_mapping_driver**:
Function providing the ability to add a mapping driver to an Entity Manager.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,12 @@ public function register(\Pimple $app)

$cache = $app['orm.cache.factory']($driver, $options[$cacheNameKey]);

if(isset($options['cache_namespace']) && $cache instanceof CacheProvider) {
$cache->setNamespace($options['cache_namespace']);
if ($cache instanceof CacheProvider) {
if (isset($options[$cacheNameKey]['namespace'])) {
$cache->setNamespace($options[$cacheNameKey]['namespace']);
} elseif (isset($options['cache_namespace'])) {
$cache->setNamespace($options['cache_namespace']);
}
}

return $app[$cacheInstanceKey] = $cache;
Expand Down Expand Up @@ -292,13 +296,18 @@ public function register(\Pimple $app)
throw new \RuntimeException('Host and port options need to be specified for redis cache');
}

/** @var \Redis $redis */
$redis = $app['orm.cache.factory.backing_redis']();
$redis->connect($cacheOptions['host'], $cacheOptions['port']);

if (isset($cacheOptions['password'])) {
$redis->auth($cacheOptions['password']);
}

if (isset($cacheOptions['database'])) {
$redis->select($cacheOptions['database']);
}

$cache = new RedisCache;
$cache->setRedis($redis);

Expand Down