Skip to content

Introduce cache-key-namespace configuration #138

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

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions config/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
// Available: null, apc, xcache, redis, memcache
'cache_provider' => null,

// A string that will act as a prefix for cached values' keys.
// When left as `null`, this will be internally defaulted to:
// "dc2_" . md5($proxyDir) . "_", see: Doctrine\ORM\Tools\Setup
'cache_key_namespace' => null,

'cache' => [
'redis' => [
'host' => '127.0.0.1',
Expand Down
18 changes: 17 additions & 1 deletion src/LaravelDoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Tools\Setup;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\EventManager;
use Illuminate\Auth\AuthManager;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -98,11 +99,15 @@ private function registerEntityManager()
{
$this->app->singleton(EntityManager::class, function ($app) {
$config = $app['config']['doctrine::doctrine'];

/** @var CacheProvider|null $cacheProvider */
$cacheProvider = $app['Mitch\LaravelDoctrine\CacheManager']->getCache($config['cache_provider']);

$metadata = Setup::createAnnotationMetadataConfiguration(
$config['metadata'],
$app['config']['app.debug'],
$config['proxy']['directory'],
$app[CacheManager::class]->getCache($config['cache_provider']),
$cacheProvider,
$config['simple_annotations']
);
$metadata->addFilter('trashed', TrashedFilter::class);
Expand All @@ -124,6 +129,17 @@ private function registerEntityManager()
$eventManager->addEventListener(Events::loadClassMetadata, $tablePrefix);
}

/*
* We need to do that here, because the namespace is defaulted in
* Setup::createAnnotationMetadataConfiguration
*/
if (
isset($config['cache_key_namespace'])
&& $cacheProvider
) {
$cacheProvider->setNamespace($config['cache_key_namespace']);
}

$eventManager->addEventListener(Events::onFlush, new SoftDeletableListener);

$entityManager = EntityManager::create($connection_config, $metadata, $eventManager);
Expand Down