Skip to content

Instantly share code, notes, and snippets.

@holtkamp
Last active January 9, 2016 14:47
Show Gist options
  • Save holtkamp/83555be2e542dca3a11e to your computer and use it in GitHub Desktop.
Save holtkamp/83555be2e542dca3a11e to your computer and use it in GitHub Desktop.
PHP-DI cache configuration with dynamic namespaces that result in specific MongoCollections
<?php
return array(
MongoClient::class => object()->constructor(get('setting.mongodb.url'), $options = array()),
MongoDB::class => object()->constructor(get(MongoClient::class), get('setting.mongodb.database')),
'*' . MongoCollection::class => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = str_replace(MongoCollection::class, '', $e->getName());
return new MongoCollection($container->get(MongoDB::class), $name = $namespace);
},
'*chainCachesCollection' => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = str_replace('chainCachesCollection', '', $e->getName());
return array(
$container->get(Doctrine\Common\Cache\ArrayCache::class),
$container->get(Doctrine\Common\Cache\ApcuCache::class),
$container->get($namespace . Doctrine\Common\Cache\MongoDBCache::class)
);
},
'*' . MongoCollection::class => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = str_replace(MongoCollection::class, '', $e->getName());
return new MongoCollection($container->get(MongoDB::class), $name = $namespace);
},
Doctrine\Common\Cache\ArrayCache::class => object()->constructor(),
Doctrine\Common\Cache\ApcuCache::class => object()->constructor(),
'*'. Doctrine\Common\Cache\MongoDBCache::class => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = str_replace(Doctrine\Common\Cache\MongoDBCache::class, '', $e->getName());
return new Doctrine\Common\Cache\MongoDBCache($container->get($namespace. MongoCollection::class));
},
'*'. Doctrine\Common\Cache\ChainCache::class => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = str_replace(Doctrine\Common\Cache\ChainCache::class, '', $e->getName());
return new Doctrine\Common\Cache\ChainCache($container->get($namespace. 'chainCachesCollection'));
},
'*CacheProvider' => function (ContainerInterface $container, RequestedEntry $e) {
$namespace = $e->getName();
$chainCache = $container->get($namespace . Doctrine\Common\Cache\ChainCache::class);
$chainCache->setNamespace($namespace);
return $chainCache;
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment