/** * Creates cache by name. * * @param string $name Name. * @param array $options Options. * * @return CacheProvider * @throws \InvalidArgumentException When cache provider with given name not found. * @throws \LogicException When no caches provided for "chain" cache. */ public function create($name, array $options = array()) { switch ($name) { case 'chain': $valid_caches = array(); foreach (array_filter($options) as $cache_name) { $valid_caches[] = self::create($cache_name); } if (!$valid_caches) { throw new \LogicException('No valid caches provided for "chain" cache.'); } $cache_driver = new ChainCache($valid_caches); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'array': $cache_driver = new ArrayCache(); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'apc': $cache_driver = new ApcCache(); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'memcache': $memcache = new \Memcache(); $memcache->connect('localhost', 11211); $cache_driver = new MemcacheCache(); $cache_driver->setMemcache($memcache); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'memcached': $memcached = new \Memcached(); $memcached->addServer('memcache_host', 11211); $cache_driver = new MemcachedCache(); $cache_driver->setMemcached($memcached); $cache_driver->setNamespace($this->_namespace); return $cache_driver; } throw new \InvalidArgumentException('Cache provider "' . $name . '" not found.'); }
/** * Create service with name. * * @param ServiceLocatorInterface $services * @param string $name * @param string $requestedName * @return mixed */ public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) { // Note that we already checked for existence in canCreateServiceWithName() $options = $this->getOptions($services, $requestedName); $storageName = $options->getStorage(); if (!$storageName) { throw new Exception\ConfigException(sprintf('Missing configuration option "storage" for cache "%s"', $requestedName)); } if (!$services->has($storageName)) { throw new Exception\ConfigException(sprintf('Cache storage "%s" does not exist for cache "%s"', $storageName, $requestedName)); } /** @var StorageInterface $storage */ $storage = $services->get($storageName); $namespace = $options->getNamespace() ?: 'DetailPersistence'; $cache = new ZendStorageCache($storage); $cache->setNamespace($namespace); if ($options->chainToArrayCache() !== false) { $arrayCache = new ArrayCache(); $arrayCache->setNamespace($namespace); // Use same namespace $cache = new ChainCache(array($arrayCache, $cache)); } return $cache; }
/** * Creates a configuration without a metadata driver. * * @param bool $isDevMode * @param string $proxyDir * @param Cache $cache * * @return Configuration */ public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null) { $proxyDir = $proxyDir ?: sys_get_temp_dir(); if ($isDevMode === false && $cache === null) { if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache')) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } elseif ($cache === null) { $cache = new ArrayCache(); } if ($cache instanceof CacheProvider) { $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions } $config = new Configuration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setProxyDir($proxyDir); $config->setProxyNamespace('DoctrineProxies'); $config->setAutoGenerateProxyClasses($isDevMode); return $config; }
/** * Get cache driver according to config.yml entry. * * Logic from Doctrine setup method * https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Setup.php#L122 * * @param array $cacheConfig * @param boolean $isDevMode * @param string|null $proxyDir * @return Cache */ protected function getManuallyDefinedCache(array $cacheConfig, $isDevMode = false, $proxyDir = null) { $proxyDir = $proxyDir ?: sys_get_temp_dir(); if ($isDevMode === false) { if (extension_loaded('apc') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'apc') { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'xcache') { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'memcache') { $memcache = new \Memcache(); $host = !empty($cacheConfig['host']) ? $cacheConfig['host'] : '127.0.0.1'; if (!empty($cacheConfig['port'])) { $memcache->connect($host, $cacheConfig['port']); } else { $memcache->connect($host); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'redis') { $redis = new \Redis(); $host = !empty($cacheConfig['host']) ? $cacheConfig['host'] : '127.0.0.1'; if (!empty($cacheConfig['port'])) { $redis->connect($host, $cacheConfig['port']); } else { $redis->connect($host); } $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } else { $cache = new ArrayCache(); } if ($cache instanceof CacheProvider) { $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions } return $cache; }
/** * Initialize the cache syntetic service and injets it in the dependency injection container * * @return Doctrine\Common\Cache\ApcCache|Doctrine\Common\Cache\ArrayCache * @todo Allow to use other cache implementation */ protected function _initCache() { if (extension_loaded('apc') && ini_get('apc.enabled')) { $cache = new ApcCache(); } else { $cache = new ArrayCache(); } $cache->setNamespace('iPMS'); return $cache; }
/** * Initialize Doctrine entity manager in DI container. * * This method can be called from InstallApp after updating * doctrine configuration. * * @param Pimple\Container $container [description] */ public function register(Container $container) { if ($container['config'] !== null && isset($container['config']["doctrine"])) { $container['em.config'] = function ($c) { $config = Setup::createAnnotationMetadataConfiguration($c['entitiesPaths'], (bool) $c['config']['devMode'], ROADIZ_ROOT . '/gen-src/Proxies', null, false); $config->setProxyDir(ROADIZ_ROOT . '/gen-src/Proxies'); $config->setProxyNamespace('Proxies'); return $config; }; $container['em'] = function ($c) { try { $c['stopwatch']->start('initDoctrine'); $em = EntityManager::create($c['config']["doctrine"], $c['em.config']); $evm = $em->getEventManager(); /* * Create dynamic dicriminator map for our Node system */ $evm->addEventListener(Events::loadClassMetadata, new DataInheritanceEvent()); $resultCacheDriver = $em->getConfiguration()->getResultCacheImpl(); if ($resultCacheDriver !== null) { $resultCacheDriver->setNamespace($c['config']["appNamespace"]); } $hydratationCacheDriver = $em->getConfiguration()->getHydrationCacheImpl(); if ($hydratationCacheDriver !== null) { $hydratationCacheDriver->setNamespace($c['config']["appNamespace"]); } $queryCacheDriver = $em->getConfiguration()->getQueryCacheImpl(); if ($queryCacheDriver !== null) { $queryCacheDriver->setNamespace($c['config']["appNamespace"]); } $metadataCacheDriver = $em->getConfiguration()->getMetadataCacheImpl(); if (null !== $metadataCacheDriver) { $metadataCacheDriver->setNamespace($c['config']["appNamespace"]); } $c['stopwatch']->stop('initDoctrine'); return $em; } catch (\PDOException $e) { $c['session']->getFlashBag()->add('error', $e->getMessage()); return null; } }; } /* * logic from Doctrine setup method * * https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Setup.php#L122 */ $container['nodesSourcesUrlCacheProvider'] = function ($c) { if (true === (bool) $c['config']['devMode']) { $cache = new ArrayCache(); } else { if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache')) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } if ($cache instanceof CacheProvider) { $cache->setNamespace($c['config']["appNamespace"] . "_nsurls"); // to avoid collisions } return $cache; }; return $container; }
public function loadDoctrineConfiguration(Application $app) { $app['doctrine.configuration'] = $app->share(function () use($app) { if (isset($app['doctrine.orm']) and true === $app['doctrine.orm']) { // Check available cache drivers if (extension_loaded('apc')) { $cache = new ApcCache(); } else { if (extension_loaded('xcache')) { $cache = new XcacheCache(); } else { if (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new MemcacheCache(); $cache->setMemcache($memcache); } else { $cache = new ArrayCache(); } } } $cache->setNamespace("dc2_"); // to avoid collisions $config = new ORMConfiguration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $chain = new DriverChain(); foreach ((array) $app['doctrine.orm.entities'] as $entity) { switch ($entity['type']) { case 'annotation': $reader = new AnnotationReader(); $driver = new AnnotationDriver($reader, (array) $entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'yml': $driver = new YamlDriver((array) $entity['path']); $driver->setFileExtension('.yml'); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver((array) $entity['path'], $entity['namespace']); $driver->setFileExtension('.xml'); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } $config->setMetadataDriverImpl($chain); $config->setProxyDir($app['doctrine.orm.proxies_dir']); $config->setProxyNamespace($app['doctrine.orm.proxies_namespace']); $config->setAutoGenerateProxyClasses($app['doctrine.orm.auto_generate_proxies']); } else { $config = new DBALConfiguration(); } return $config; }); }
public function testDoesNotModifyCacheNamespace() { $cache = new ArrayCache(); $cache->setNamespace('foo'); new DefaultRegion('bar', $cache); new DefaultRegion('baz', $cache); $this->assertSame('foo', $cache->getNamespace()); }