Skip to content

Add an option to create separate cache for Doctrine's query results #141

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 4 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
12 changes: 12 additions & 0 deletions config/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
// 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,

// When set to `null`, then the doctrine's query result cache will be the same
// as the metadata and query caches. If you need to be able to flush query result
// cache without touching the metadata and query caches, then set the following
// namespace to something different from the value in the `cache_key_namespace`
// config.
'result_cache_key_namespace' => null,

'cache' => [
'redis' => [
'host' => '127.0.0.1',
Expand Down
7 changes: 6 additions & 1 deletion src/CacheManager.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Mitch\LaravelDoctrine;
<?php namespace Mitch\LaravelDoctrine;

use Doctrine\Common\Cache\CacheProvider;
use Mitch\LaravelDoctrine\Cache\Provider;

class CacheManager
Expand All @@ -11,6 +12,10 @@ public function __construct($config)
$this->config = $config;
}

/**
* @param string $type
* @return CacheProvider|null
*/
public function getCache($type)
{
foreach ($this->providers as $provider)
Expand Down
30 changes: 29 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,17 @@ private function registerEntityManager()
{
$this->app->singleton(EntityManager::class, function ($app) {
$config = $app['config']['doctrine::doctrine'];

/** @var CacheManager $cacheManager */
$cacheManager = $app['Mitch\LaravelDoctrine\CacheManager'];

$cacheProvider = $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 +131,27 @@ 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']);
}

if (
isset($config['result_cache_key_namespace'])
&& isset($config['cache_provider'])
) {
$resultCacheProvider = $cacheManager->getCache($config['cache_provider']);
$resultCacheProvider->setNamespace($config['result_cache_key_namespace']);

$metadata->setResultCacheImpl($resultCacheProvider);
}

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

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