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

Added options to set namespace and database for redis cache drivers #85

Open
wants to merge 15 commits into
base: master
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# System, IDE, ...
.DS_Store
.idea
.vagrant
.bundle

# Composer
composer.phar
composer.lock
vendor
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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 @@ -234,6 +236,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
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
"require": {
"php": ">=5.3.3",
"pimple/pimple": ">=2.1,<4",
"doctrine/orm": "~2.3"
"doctrine/orm": "^2.3",
"symfony/cache": "^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"doctrine/annotations": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -28,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "2.5.x-dev"
}
}
}
127 changes: 35 additions & 92 deletions src/Dflydev/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@

namespace Dflydev\Provider\DoctrineOrm;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\Common\Cache\MemcacheCache;
use Doctrine\Common\Cache\MemcachedCache;
use Doctrine\Common\Cache\CouchbaseCache;
use Doctrine\Common\Cache\XcacheCache;
use Doctrine\Common\Cache\RedisCache;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\ORMSetup;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\Driver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver;
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\CouchbaseBucketAdapter;
use Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

/**
* Doctrine ORM Pimple Service Provider.
Expand Down Expand Up @@ -163,20 +163,16 @@ public function register(Container $container)
}

switch ($entity['type']) {
case 'annotation':
$useSimpleAnnotationReader =
isset($entity['use_simple_annotation_reader'])
? $entity['use_simple_annotation_reader']
: true;
$driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'yml':
$driver = new YamlDriver($entity['path']);
case 'attribute':
$driver = new AttributeDriver(array($entity['path']));
$chain->addDriver($driver, $entity['namespace']);
break;
case 'simple_yml':
$driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace']));
case 'annotation':
$annotationsCache = null;
if (array_key_exists('cache', $entity) && $entity['cache'] !== null) {
$annotationsCache = $entity['cache'];
}
$driver = ORMSetup::createDefaultAnnotationDriver((array) $entity['path'], $annotationsCache);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'xml':
Expand Down Expand Up @@ -213,10 +209,10 @@ public function register(Container $container)
};

$container['orm.cache.configurer'] = $container->protect(function ($name, Configuration $config, $options) use ($container) {
$config->setMetadataCacheImpl($container['orm.cache.locator']($name, 'metadata', $options));
$config->setQueryCacheImpl($container['orm.cache.locator']($name, 'query', $options));
$config->setResultCacheImpl($container['orm.cache.locator']($name, 'result', $options));
$config->setHydrationCacheImpl($container['orm.cache.locator']($name, 'hydration', $options));
$config->setMetadataCache($container['orm.cache.locator']($name, 'metadata', $options));
$config->setQueryCache($container['orm.cache.locator']($name, 'query', $options));
$config->setResultCache($container['orm.cache.locator']($name, 'result', $options));
$config->setHydrationCache($container['orm.cache.locator']($name, 'hydration', $options));
});

$container['orm.cache.locator'] = $container->protect(function ($name, $cacheName, $options) use ($container) {
Expand Down Expand Up @@ -245,32 +241,9 @@ public function register(Container $container)

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

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

return $container[$cacheInstanceKey] = $cache;
});

$container['orm.cache.factory.backing_memcache'] = $container->protect(function () {
return new \Memcache;
});

$container['orm.cache.factory.memcache'] = $container->protect(function ($cacheOptions) use ($container) {
if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) {
throw new \RuntimeException('Host and port options need to be specified for memcache cache');
}

/** @var \Memcache $memcache */
$memcache = $container['orm.cache.factory.backing_memcache']();
$memcache->connect($cacheOptions['host'], $cacheOptions['port']);

$cache = new MemcacheCache;
$cache->setMemcache($memcache);

return $cache;
});

$container['orm.cache.factory.backing_memcached'] = $container->protect(function () {
return new \Memcached;
});
Expand All @@ -284,8 +257,7 @@ public function register(Container $container)
$memcached = $container['orm.cache.factory.backing_memcached']();
$memcached->addServer($cacheOptions['host'], $cacheOptions['port']);

$cache = new MemcachedCache;
$cache->setMemcached($memcached);
$cache = new MemcachedAdapter($memcached, $cacheOptions['namespace']);

return $cache;
});
Expand All @@ -307,58 +279,29 @@ public function register(Container $container)
$redis->auth($cacheOptions['password']);
}

$cache = new RedisCache;
$cache->setRedis($redis);
if (isset($cacheOptions['database'])) {
$redis->select($cacheOptions['database']);
}

return $cache;
});
$cache = new RedisAdapter($redis, $cacheOptions['namespace']);

$container['orm.cache.factory.array'] = $container->protect(function () {
return new ArrayCache;
return $cache;
});

$container['orm.cache.factory.apc'] = $container->protect(function () {
return new ApcCache;
$container['orm.cache.factory.array'] = $container->protect(function ($cacheOptions) {
return new ArrayAdapter();
});

$container['orm.cache.factory.xcache'] = $container->protect(function () {
return new XcacheCache;
$container['orm.cache.factory.apc'] = $container->protect(function ($cacheOptions) {
return new ApcuAdapter($cacheOptions['namespace']);
});

$container['orm.cache.factory.filesystem'] = $container->protect(function ($cacheOptions) {
if (empty($cacheOptions['path'])) {
throw new \RuntimeException('FilesystemCache path not defined');
}

$cacheOptions += array(
'extension' => FilesystemCache::EXTENSION,
'umask' => 0002,
);
return new FilesystemCache($cacheOptions['path'], $cacheOptions['extension'], $cacheOptions['umask']);
});

$container['orm.cache.factory.couchbase'] = $container->protect(function($cacheOptions){
$host='';
$bucketName='';
$user='';
$password='';
if (empty($cacheOptions['host'])) {
$host='127.0.0.1';
}
if (empty($cacheOptions['bucket'])) {
$bucketName='default';
}
if (!empty($cacheOptions['user'])) {
$user=$cacheOptions['user'];
}
if (!empty($cacheOptions['password'])) {
$password=$cacheOptions['password'];
}

$couchbase = new \Couchbase($host,$user,$password,$bucketName);
$cache = new CouchbaseCache();
$cache->setCouchbase($couchbase);
return $cache;
return new FilesystemAdapter($cacheOptions['namespace'], 0, $cacheOptions['path']);
});

$container['orm.cache.factory'] = $container->protect(function ($driver, $cacheOptions) use ($container) {
Expand Down
Loading