Skip to content

Commit

Permalink
[Doctrine]: allow use Redis for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasPilar committed Aug 7, 2018
1 parent a3a907e commit 27b9214
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
18 changes: 13 additions & 5 deletions docs/en/index.md
Expand Up @@ -69,19 +69,27 @@ doctrine:
functions:
CAST: \App\Doctrine\MySQL\Functions\Cast

metadataCache: default
queryCache: default
resultCache: default
hydrationCache: default
metadataCache: default # options: FALSE, default (Nette Storage), redis
queryCache: default # options: FALSE, default (Nette Storage), redis
resultCache: default # options: FALSE, default (Nette Storage), redis
hydrationCache: default # options: FALSE, default (Nette Storage), redis
secondLevelCache:
enabled: FALSE
driver: default # options: FALSE, default (Nette Storage), redis
factoryClass: \Doctrine\ORM\Cache\DefaultCacheFactory::class
driver: default
regions:
defaultLifetime: 3600
defaultLockLifetime: 60
fileLockRegionDirectory: %tempDir%/cache/Doctrine.Cache.Locks
logging: %debugMode%

cache:
redis: # optional configuration for redis cache
host: 127.0.0.1
port: 6379
timeout: 0
reserved: null
retryInterval: 5
```

### Modular entities
Expand Down
39 changes: 30 additions & 9 deletions src/Adapter/Nette/DI/DoctrineExtension.php
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\RedisCache;
use Doctrine\Common\EventManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Connection;
Expand Down Expand Up @@ -48,6 +49,7 @@
use Portiny\Doctrine\Cache\DefaultCache;
use Portiny\Doctrine\Contract\Provider\ClassMappingProviderInterface;
use Portiny\Doctrine\Contract\Provider\EntitySourceProviderInterface;
use Redis;
use Symfony\Component\Console\Helper\HelperSet;
use Tracy\IBarPanel;

Expand Down Expand Up @@ -95,6 +97,9 @@ class DoctrineExtension extends CompilerExtension
'fileLockRegionDirectory' => '%tempDir%/cache/Doctrine.Cache.Locks',
'logging' => '%debugMode%',
],
'cache' => [
'redis' => [],
],
];

private $entitySources = [];
Expand Down Expand Up @@ -192,18 +197,11 @@ public function beforeCompile(): void
$name = $config['prefix'];

$builder = $this->getContainerBuilder();
$cache = $this->getCache($name, $builder, 'default');

$configDefinition = $builder->getDefinition($name . '.config')
->setFactory(
'\Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration',
[
array_values($this->entitySources),
$config['debug'],
$config['proxyDir'],
$config['metadataCache'] !== FALSE ? $cache : NULL,
FALSE,
]
[array_values($this->entitySources), $config['debug'], $config['proxyDir'], NULL, FALSE]
)
->addSetup('setNamingStrategy', ['@' . $name . '.namingStrategy']);

Expand Down Expand Up @@ -296,16 +294,39 @@ private function getCache(string $prefix, ContainerBuilder $containerBuilder, st
$cacheClass = ArrayCache::class;
if ($cacheType) {
switch ($cacheType) {
case 'redis':
$cacheClass = RedisCache::class;
break;

case 'default':
default:
$cacheClass = DefaultCache::class;
break;
}
}

$containerBuilder->addDefinition($prefix . '.cache')
$cacheDefinition = $containerBuilder->addDefinition($prefix . '.cache')
->setType($cacheClass);

if ($cacheType === 'redis') {
$config = $this->parseConfig();
$redisConfig = $config['cache']['redis'];

$containerBuilder->addDefinition($prefix . '.redis')
->setType(Redis::class)
->setAutowired(FALSE)
->addSetup('connect', [
$redisConfig['host'] ?? '127.0.0.1',
$redisConfig['port'] ?? null,
$redisConfig['timeout'] ?? 0.0,
$redisConfig['reserved'] ?? null,
$redisConfig['retryInterval'] ?? 0,
])
->addSetup('select', [$redisConfig['database'] ?? 1]);

$cacheDefinition->addSetup('setRedis', ['@' . $prefix . '.redis']);
}

return '@' . $prefix . '.cache';
}

Expand Down

0 comments on commit 27b9214

Please sign in to comment.