private static function driver() { $cacheType = strtolower(Config::get('cache.type')); if ($cacheType == 'apc') { $cacheDriver = new ApcCache(); return $cacheDriver; } if ($cacheType == 'memcache') { $memcacheConfig = Config::get('cache.memcache'); $memcache = new Memcache(); $memcache->connect($memcacheConfig['host'], $memcacheConfig['port']); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; } if ($cacheType == 'xcache') { $cacheDriver = new XcacheCache(); return $cacheDriver; } if ($cacheType == 'redis') { $redisConfig = Config::get('cache.redis'); $redis = new Redis(); $redis->connect($redisConfig['host'], $redisConfig['port']); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($redis); return $cacheDriver; } return null; }
/** * Registers services on the given app. * * This method should only be used to configure services and parameters. * It should not get services. * @param Application $app */ public function register(Application $app) { parent::register($app); $app['cache'] = $app->share(function ($app) { $config = $app['config']; if (!array_key_exists('cache.driver', $config)) { return new ArrayCache(); } switch ($config['cache.driver']) { case 'array': return new ArrayCache(); case 'redis': $cache = new RedisCache(); if (isset($config['cache.redis'])) { $redis = new \Redis(); $redis->connect($config['cache.redis']['host'], $config['cache.redis']['port']); $cache->setRedis($redis); } else { $cache->setRedis($app['redis']); } return $cache; case 'xcache': return new XcacheCache(); default: throw new \RuntimeException('Unknown cache driver: ' . $config['cache.driver']); } }); }
/** * @test */ public function WithRedisType_Build_ReturnRedis() { $redisCache = new RedisCache(); $redisCache->setRedis(new RedisSpy()); $cache = $this->cacheBuilder->withCacheProvider($redisCache)->build(); $this->assertAttributeInstanceOf('Doctrine\\Common\\Cache\\RedisCache', 'cache', $cache); }
/** * @param OutPointInterface[] $requiredOutpoints * @return UtxoInterface[] */ public function fetchView(array $requiredOutpoints) { try { $utxos = []; $required = []; $cacheHits = []; foreach ($requiredOutpoints as $c => $outpoint) { $key = $this->outpointSerializer->serialize($outpoint)->getBinary(); if ($this->set->contains($key)) { list($value, $scriptPubKey) = $this->set->fetch($key); $cacheHits[] = $key; $utxos[] = new Utxo($outpoint, new TransactionOutput($value, new Script(new Buffer($scriptPubKey)))); } else { $required[] = $outpoint; } } if (empty($required) === false) { $utxos = array_merge($utxos, $this->db->fetchUtxoDbList($this->outpointSerializer, $required)); } if ($this->caching) { $this->cacheHits = $cacheHits; } return $utxos; } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; throw new \RuntimeException('Failed to find UTXOS in set'); } }
/** * @return CacheProvider */ protected function getCacheProvider() { $config = $this->getConfig()->get('orm.cache'); switch ($config['provider']) { case 'array': return new ArrayCache(); case 'filesystem': return new FilesystemCache($config[$config['provider']]['directory'], $config[$config['provider']]['extension']); case 'redis': $redis = new \Redis(); $redis->connect($config[$config['provider']]['host'], $config[$config['provider']]['port']); $redis->select($config[$config['provider']]['dbindex']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; case 'memcached': $memcache = new \Memcache(); $memcache->connect($config[$config['provider']]['host'], $config[$config['provider']]['port'], $config[$config['provider']]['weight']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; default: return null; } }
/** * @return RedisCache */ private function getCache() { $redis = $this->getRedis(); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }
/** * @param $env * @return \DI\Container */ public function getContainer($env) { $builder = new ContainerBuilder(); foreach (glob(APP_HOME . '/conf/conf.d/*.conf.php') as $filename) { $builder->addDefinitions($filename); } $builder->addDefinitions(APP_HOME . '/conf/config.' . $env . '.php'); switch ($env) { case EnvironmentEnum::DEV: $builder->setDefinitionCache(new ArrayCache()); break; case EnvironmentEnum::PROD: $redis = new Redis(); $redis->pconnect('localhost'); $redis->setOption(Redis::OPT_PREFIX, 'homeservice:cache:di:'); $cache = new RedisCache(); $cache->setRedis($redis); $builder->setDefinitionCache($cache); $builder->writeProxiesToFile(true, APP_HOME . '/var/cache/proxies'); break; } $builder->useAnnotations(true); $container = $builder->build(); return $container; }
/** * {@inheritdoc} */ public function getAdapter(array $config) { $redis = new \Redis(); $redis->connect($config['host'], $config['port']); $client = new RedisCache(); $client->setRedis($redis); return new DoctrineCachePool($client); }
/** * Sets up and returns the CacheProvider * @param $config * @return \Doctrine\Common\Cache\CacheProvider */ protected function initialize($config) { $redis = new \Redis(); $redis->connect($config['host'], $config['port']); $redis->select($config['database']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }
/** * Return's the new cache instance. * * @param array $configuration The cache configuration * * @return \Doctrine\Common\Cache\CacheProvider The cache instance */ public static function get(array $configuration = array()) { if (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect($configuration[CacheKeys::HOST]); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; } }
/** * @param ContainerInterface $container * * @return RedisCache */ public function __invoke(ContainerInterface $container) { if ($container->has(\Redis::class)) { $redis = $container->get(\Redis::class); } else { $redis = $this->getRedisConnection($this->getRedisConfig($container)); } $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }
/** * @param ContainerInterface $container * @return RedisCache $cache */ public function __invoke(ContainerInterface $container) { $config = $container->has('config') ? $container->get('config') : []; if (!isset($config['doctrine'])) { throw new ServiceNotCreatedException('Missing Doctrine Cache configuration'); } $redis = new \Redis(); $redis->connect($config['doctrine']['cache']['redis']['host'], $config['doctrine']['cache']['redis']['port']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }
protected function drivers(Container $app) { $app['cache.driver.memcached'] = $app->protect(function ($options) { $servers = $options['servers']; $memcached = new \Memcached(); foreach ($servers as $server) { $memcached->addServer($server['host'], $server['port']); } $cacheDriver = new MemcachedCache(); $cacheDriver->setMemcached($memcached); return $cacheDriver; }); $app['cache.driver.memcache'] = $app->protect(function ($options) { $options = array_merge(['host' => '127.0.0.1', 'port' => 11211], $options); $memcache = new \Memcache(); $memcache->connect($options['host'], $options['port']); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; }); $app['cache.driver.file'] = $app->protect(function ($options) { if (empty($options['path']) || false === is_dir($options['path'])) { throw new \InvalidArgumentException('You must specify "path" for Filesystem.'); } return new FilesystemCache($options['path']); }); $app['cache.driver.redis'] = $app->protect(function ($options) { $options = array_merge(['host' => '127.0.0.1', 'port' => 6379], $options); $redis = new \Redis(); $redis->connect($options['host'], $options['port']); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($redis); return $cacheDriver; }); $app['cache.driver.mongodb'] = $app->protect(function ($options) { if (empty($options['server']) || empty($options['name']) || empty($options['collection'])) { throw new \InvalidArgumentException('You must specify "server", "name" and "collection" for MongoDB.'); } $client = new \MongoClient($options['server']); $db = new \MongoDB($client, $options['name']); $collection = new \MongoCollection($db, $options['collection']); return new MongoDBCache($collection); }); $app['cache.driver.array'] = $app->protect(function () { return new ArrayCache(); }); $app['cache.driver.apc'] = $app->protect(function () { return new ApcuCache(); }); $app['cache.driver.xcache'] = $app->protect(function () { return new XcacheCache(); }); }
/** * @throws DriverNotFound * @return RedisCache */ public function resolve() { if (extension_loaded('redis')) { $cache = new RedisCache(); $redis = new Redis(); $redis->connect($this->config['host'], $this->config['port']); $redis->select($this->config['database']); $cache->setRedis($redis); return $cache; } throw new DriverNotFound('Redis extension was not found'); }
public function make($config = null) { if (!extension_loaded('redis')) { throw new \RuntimeException('Redis extension was not loaded.'); } $redis = new Redis(); $redis->connect($config['host'], $config['port']); if (isset($config['database'])) { $redis->select($config['database']); } $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }
public function createService(ServiceLocatorInterface $serviceLocator) { $redis = $serviceLocator->get('redis'); $config = $serviceLocator->get('ApplicationConfig'); if (is_array($config) && isset($config['tenant_id'])) { if ($redis instanceof \Redis) { $cache = new RedisCache(); $cache->setNamespace(sprintf('stokq.doctrine.%s', $config['tenant_id'])); $cache->setRedis($redis); return $cache; } } else { throw new \RuntimeException("Unable to get `tenant_id` from config."); } throw new \RuntimeException("Invalid redis instance returned."); }
/** * @inheritdoc */ public function getLists($cache = true) { $id = 'newsletter.lists'; if ($cache && $this->cache->get($id)) { return json_decode($this->cache->get($id), true); } foreach ($this->drivers as $driver) { try { $this->checkDriverConfiguration($driver); } catch (NewsletterException $ex) { unset($this->drivers[$driver->getName()]); } } $result = []; if (is_array($this->lists) && count($this->lists) > 0) { $result = $this->lists; } else { foreach ($this->drivers as $driver) { $result[$driver->getName()] = $driver->getLists(); } } if ($cache) { $this->cache->set($id, json_encode($result)); } return json_decode($this->cache->get($id), true); }
private function initORM() { $config_pff = ServiceContainer::get('config'); if (true === $config_pff->getConfigData('development_environment')) { $cache = new ArrayCache(); } elseif ($this->redis) { $redis = new \Redis(); if (!$redis->connect($this->redis_host, $this->redis_port)) { throw new PffException("Cannot connect to redis", 500); } if ($this->redis_password != '') { if (!$redis->auth($this->redis_password)) { throw new PffException("Cannot auth to redis", 500); } } $cache = new RedisCache(); $cache->setRedis($redis); $cache->setNamespace($this->_app->getConfig()->getConfigData('app_name')); } else { $cache = new ApcuCache(); $cache->setNamespace($this->_app->getConfig()->getConfigData('app_name')); } $config = new Configuration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $driverImpl = $config->newDefaultAnnotationDriver(ROOT . DS . 'app' . DS . 'models'); $config->setMetadataDriverImpl($driverImpl); $config->setQueryCacheImpl($cache); $config->setProxyDir(ROOT . DS . 'app' . DS . 'proxies'); $config->setProxyNamespace('pff\\proxies'); if (true === $config_pff->getConfigData('development_environment')) { $config->setAutoGenerateProxyClasses(true); $connectionOptions = $config_pff->getConfigData('databaseConfigDev'); } else { $config->setAutoGenerateProxyClasses(false); $connectionOptions = $config_pff->getConfigData('databaseConfig'); } $this->db = EntityManager::create($connectionOptions, $config); ServiceContainer::set()['dm'] = $this->db; $platform = $this->db->getConnection()->getDatabasePlatform(); $platform->registerDoctrineTypeMapping('enum', 'string'); }
/** * Puts data into the cache. * * $id can be specified as an array of key-value pairs: array( 'alpha' => 'xyz', 'beta' => 'qrs', 'gamma' => 'lmo', ... ) * * * @param string|array $id The cache id or array of key-value pairs * @param mixed $data The cache entry/data. * @param int $lifeTime The cache lifetime. Sets a specific lifetime for this cache entry. Defaults to 0, or "never expire" * * @return boolean|boolean[] TRUE if the entry was successfully stored in the cache, FALSE otherwise. */ public function set($id, $data = null, $lifeTime = self::DEFAULT_CACHE_TTL) { if (is_array($id) && null === $data) { $_result = array(); foreach ($id as $_key => $_value) { $_result[$_key] = $this->_store->save($_key, $_value, $lifeTime); } return $_result; } return $this->_store->save($id, $data, $lifeTime); }
protected function setUp() { if (!class_exists('\\Onoi\\Cache\\CacheFactory')) { $this->markTestSkipped('CacheFactory is not available'); } if (!class_exists('\\Redis')) { $this->markTestSkipped('Requires redis php-class/extension to be available'); } $redis = new \Redis(); if (!$redis->connect('127.0.0.1')) { $this->markTestSkipped('Cannot connect to redis'); } if (!class_exists('\\Doctrine\\Common\\Cache\\RedisCache')) { $this->markTestSkipped('RedisCache is not available'); } $redisCache = new RedisCache(); $redisCache->setRedis($redis); $cacheFactory = new CacheFactory(); $this->cache = $cacheFactory->newDoctrineCache($redisCache); }
private function buildMemcachedCache() { if (null === $this->host) { throw new HostShouldBeProvidedException(); } if (null === $this->port) { $this->port = Memcached::DEFAULT_PORT; } $this->server->addserver($this->host, $this->port); $this->cacheProvider->setMemcached($this->server); }
/** * Get our Redis instance * @since Version 3.9.1 * @return \Doctrine\Common\Cache\RedisCache * @param boolean $reload Don't fetch the cache handler from the registry and instead create a new instance */ public static function getRedis($reload = false) { if (!extension_loaded("redis") || defined("NOREDIS") && NOREDIS == true) { return new NullCacheDriver(); } if (defined("PHPUNIT_RAILPAGE_TESTSUITE")) { return new NullCacheDriver(); } $Registry = Registry::getInstance(); $Config = self::getConfig(); if ($reload) { $Registry->remove("redis"); } try { $cacheDriver = $Registry->get("redis"); } catch (Exception $e) { $Redis = new Redis(); $Redis->connect($Config->Memcached->Host, 6379); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($Redis); $Registry->set("redis", $cacheDriver); } return $cacheDriver; }
/** * @param string $namespace * @return void */ public function setNamespace($namespace) { parent::setNamespace($namespace); $this->internalNamespace = $namespace; }
/** * Automatically picks the cache mechanism to use. If you pick one manually it will use that * If there is no config option for $driver in the config, or it's set to 'auto', it will * pick the best option based on which cache extensions are installed. * * @return DoctrineCache\CacheProvider The cache driver to use */ public function getCacheDriver() { $setting = $this->driver_setting; $driver_name = 'file'; if (!$setting || $setting == 'auto') { if (extension_loaded('apcu')) { $driver_name = 'apcu'; } elseif (extension_loaded('apc')) { $driver_name = 'apc'; } elseif (extension_loaded('wincache')) { $driver_name = 'wincache'; } elseif (extension_loaded('xcache')) { $driver_name = 'xcache'; } } else { $driver_name = $setting; } $this->driver_name = $driver_name; switch ($driver_name) { case 'apc': $driver = new DoctrineCache\ApcCache(); break; case 'apcu': $driver = new DoctrineCache\ApcuCache(); break; case 'wincache': $driver = new DoctrineCache\WinCacheCache(); break; case 'xcache': $driver = new DoctrineCache\XcacheCache(); break; case 'memcache': $memcache = new \Memcache(); $memcache->connect($this->config->get('system.cache.memcache.server', 'localhost'), $this->config->get('system.cache.memcache.port', 11211)); $driver = new DoctrineCache\MemcacheCache(); $driver->setMemcache($memcache); break; case 'memcached': $memcached = new \Memcached(); $memcached->addServer($this->config->get('system.cache.memcached.server', 'localhost'), $this->config->get('system.cache.memcached.port', 11211)); $driver = new DoctrineCache\MemcachedCache(); $driver->setMemcached($memcached); break; case 'redis': $redis = new \Redis(); $socket = $this->config->get('system.cache.redis.socket', false); if ($socket) { $redis->connect($socket); } else { $redis->connect($this->config->get('system.cache.redis.server', 'localhost'), $this->config->get('system.cache.redis.port', 6379)); } $driver = new DoctrineCache\RedisCache(); $driver->setRedis($redis); break; default: $driver = new DoctrineCache\FilesystemCache($this->cache_dir); break; } return $driver; }
public function register(Container $app) { $app['caches.options.initializer'] = $app->protect(function () use($app) { static $initialized = false; if ($initialized) { return; } $initialized = true; if (!isset($app['caches.options'])) { $app['caches.options'] = ['default' => isset($app['cache.options']) ? $app['cache.options'] : []]; } $app['caches.options'] = array_map(function ($options) use($app) { return array_replace($app['cache.default_options'], is_array($options) ? $options : ['driver' => $options]); }, $app['caches.options']); if (!isset($app['caches.default'])) { $app['caches.default'] = array_keys(array_slice($app['caches.options'], 0, 1))[0]; } }); $app['caches'] = function (Container $app) { $app['caches.options.initializer'](); $container = new Container(); foreach ($app['caches.options'] as $name => $options) { $container[$name] = function () use($app, $options) { $cache = $app['cache.factory']($options['driver'], $options); $cache->setNamespace($options['namespace']); return $cache; }; } return $container; }; $app['cache.filesystem'] = $app->protect(function ($options) { if (empty($options['cache_dir']) || false === is_dir($options['cache_dir'])) { throw new \InvalidArgumentException('You must specify "cache_dir" for Filesystem.'); } return new FilesystemCache($options['cache_dir']); }); $app['cache.array'] = $app->protect(function () { return new ArrayCache(); }); $app['cache.apcu'] = $app->protect(function () { return new ApcuCache(); }); $app['cache.mongodb'] = $app->protect(function ($options) { if (empty($options['server']) || empty($options['name']) || empty($options['collection'])) { throw new \InvalidArgumentException('You must specify "server", "name" and "collection" for MongoDB.'); } $client = new \MongoClient($options['server']); $db = new \MongoDB($client, $options['name']); $collection = new \MongoCollection($db, $options['collection']); return new MongoDBCache($collection); }); $app['cache.redis'] = $app->protect(function ($options) { if (empty($options['host']) || empty($options['port'])) { throw new \InvalidArgumentException('You must specify "host" and "port" for Redis.'); } $redis = new \Redis(); $redis->connect($options['host'], $options['port']); if (isset($options['password'])) { $redis->auth($options['password']); } $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }); $app['cache.xcache'] = $app->protect(function () { return new XcacheCache(); }); $app['cache.factory'] = $app->protect(function ($driver, $options) use($app) { switch ($driver) { case 'array': return $app['cache.array'](); break; case 'apcu': return $app['cache.apcu'](); break; case 'redis': return $app['cache.redis']($options); break; case 'xcache': return $app['cache.xcache'](); break; case 'mongodb': return $app['cache.mongodb']($options); break; case 'filesystem': return $app['cache.filesystem']($options); break; } throw new \RuntimeException(); }); // shortcuts for the "first" cache $app['cache'] = function (Container $app) { $caches = $app['caches']; return $caches[$app['caches.default']]; }; $app['cache.default_options'] = ['driver' => 'array', 'namespace' => null]; }
/** * Get redis cache driver * * @return RedisCache|null */ private function getRedis() { if (isset($this->configuration['redis']) && isset($this->configuration['redis']['host']) && isset($this->configuration['redis']['port'])) { $redis = new \Redis(); $redis->connect($this->configuration['redis']['host'], $this->configuration['redis']['port']); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($redis); return $cacheDriver; } return null; }
public function register(Application $app) { foreach ($this->getOrmDefaults() as $key => $value) { if (!isset($app[$key])) { $app[$key] = $value; } } $app['orm.em.default_options'] = array('connection' => 'default', 'mappings' => array(), 'types' => array()); $app['orm.ems.options.initializer'] = $app->protect(function () use($app) { static $initialized = false; if ($initialized) { return; } $initialized = true; if (!isset($app['orm.ems.options'])) { $app['orm.ems.options'] = array('default' => isset($app['orm.em.options']) ? $app['orm.em.options'] : array()); } $tmp = $app['orm.ems.options']; foreach ($tmp as $name => &$options) { $options = array_replace($app['orm.em.default_options'], $options); if (!isset($app['orm.ems.default'])) { $app['orm.ems.default'] = $name; } } $app['orm.ems.options'] = $tmp; }); $app['orm.em_name_from_param_key'] = $app->protect(function ($paramKey) use($app) { $app['orm.ems.options.initializer'](); if (isset($app[$paramKey])) { return $app[$paramKey]; } return $app['orm.ems.default']; }); $app['orm.ems'] = $app->share(function ($app) { $app['orm.ems.options.initializer'](); $ems = new \Pimple(); foreach ($app['orm.ems.options'] as $name => $options) { if ($app['orm.ems.default'] === $name) { // we use shortcuts here in case the default has been overridden $config = $app['orm.em.config']; } else { $config = $app['orm.ems.config'][$name]; } $ems[$name] = $app->share(function ($ems) use($app, $options, $config) { return EntityManager::create($app['dbs'][$options['connection']], $config, $app['dbs.event_manager'][$options['connection']]); }); } return $ems; }); $app['orm.ems.config'] = $app->share(function ($app) { $app['orm.ems.options.initializer'](); $configs = new \Pimple(); foreach ($app['orm.ems.options'] as $name => $options) { $config = new Configuration(); $app['orm.cache.configurer']($name, $config, $options); $config->setProxyDir($app['orm.proxies_dir']); $config->setProxyNamespace($app['orm.proxies_namespace']); $config->setAutoGenerateProxyClasses($app['orm.auto_generate_proxies']); $config->setCustomStringFunctions($app['orm.custom.functions.string']); $config->setCustomNumericFunctions($app['orm.custom.functions.numeric']); $config->setCustomDatetimeFunctions($app['orm.custom.functions.datetime']); $config->setCustomHydrationModes($app['orm.custom.hydration_modes']); $config->setClassMetadataFactoryName($app['orm.class_metadata_factory_name']); $config->setDefaultRepositoryClassName($app['orm.default_repository_class']); $config->setEntityListenerResolver($app['orm.entity_listener_resolver']); $config->setRepositoryFactory($app['orm.repository_factory']); $config->setNamingStrategy($app['orm.strategy.naming']); $config->setQuoteStrategy($app['orm.strategy.quote']); $chain = $app['orm.mapping_driver_chain.locator']($name); foreach ((array) $options['mappings'] as $entity) { if (!is_array($entity)) { throw new \InvalidArgumentException("The 'orm.em.options' option 'mappings' should be an array of arrays."); } if (!empty($entity['resources_namespace'])) { $entity['path'] = $app['psr0_resource_locator']->findFirstDirectory($entity['resources_namespace']); } if (isset($entity['alias'])) { $config->addEntityNamespace($entity['alias'], $entity['namespace']); } switch ($entity['type']) { case 'annotation': $config->newDefaultAnnotationDriver((array) $entity['path']); $driver = new MappingDriverChain(); $chain->addDriver(new AnnotationDriver(new AnnotationReader(), $entity['path']), $entity['namespace']); break; case 'yml': $driver = new YamlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_yml': $driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_xml': $driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'php': $driver = new StaticPHPDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } //$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->setMetadataDriverImpl($chain); foreach ((array) $options['types'] as $typeName => $typeClass) { if (Type::hasType($typeName)) { Type::overrideType($typeName, $typeClass); } else { Type::addType($typeName, $typeClass); } } $configs[$name] = $config; } return $configs; }); $app['orm.cache.configurer'] = $app->protect(function ($name, Configuration $config, $options) use($app) { $config->setMetadataCacheImpl($app['orm.cache.locator']($name, 'metadata', $options)); $config->setQueryCacheImpl($app['orm.cache.locator']($name, 'query', $options)); $config->setResultCacheImpl($app['orm.cache.locator']($name, 'result', $options)); $config->setHydrationCacheImpl($app['orm.cache.locator']($name, 'hydration', $options)); }); $app['orm.cache.locator'] = $app->protect(function ($name, $cacheName, $options) use($app) { $cacheNameKey = $cacheName . '_cache'; if (!isset($options[$cacheNameKey])) { $options[$cacheNameKey] = $app['orm.default_cache']; } if (isset($options[$cacheNameKey]) && !is_array($options[$cacheNameKey])) { $options[$cacheNameKey] = array('driver' => $options[$cacheNameKey]); } if (!isset($options[$cacheNameKey]['driver'])) { throw new \RuntimeException("No driver specified for '{$cacheName}'"); } $driver = $options[$cacheNameKey]['driver']; $cacheInstanceKey = 'orm.cache.instances.' . $name . '.' . $cacheName; if (isset($app[$cacheInstanceKey])) { return $app[$cacheInstanceKey]; } $cache = $app['orm.cache.factory']($driver, $options[$cacheNameKey]); if (isset($options['cache_namespace']) && $cache instanceof CacheProvider) { $cache->setNamespace($options['cache_namespace']); } return $app[$cacheInstanceKey] = $cache; }); $app['orm.cache.factory.backing_memcache'] = $app->protect(function () { return new \Memcache(); }); $app['orm.cache.factory.memcache'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcache cache'); } $memcache = $app['orm.cache.factory.backing_memcache'](); $memcache->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }); $app['orm.cache.factory.backing_memcached'] = $app->protect(function () { return new \Memcached(); }); $app['orm.cache.factory.memcached'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcached cache'); } $memcached = $app['orm.cache.factory.backing_memcached'](); $memcached->addServer($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }); $app['orm.cache.factory.backing_redis'] = $app->protect(function () { return new \Redis(); }); $app['orm.cache.factory.redis'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for redis cache'); } $redis = $app['orm.cache.factory.backing_redis'](); $redis->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }); $app['orm.cache.factory.array'] = $app->protect(function () { return new ArrayCache(); }); $app['orm.cache.factory.apc'] = $app->protect(function () { return new ApcCache(); }); $app['orm.cache.factory.xcache'] = $app->protect(function () { return new XcacheCache(); }); $app['orm.cache.factory.filesystem'] = $app->protect(function ($cacheOptions) { if (empty($cacheOptions['path'])) { throw new \RuntimeException('FilesystemCache path not defined'); } return new FilesystemCache($cacheOptions['path']); }); $app['orm.cache.factory'] = $app->protect(function ($driver, $cacheOptions) use($app) { switch ($driver) { case 'array': return $app['orm.cache.factory.array'](); case 'apc': return $app['orm.cache.factory.apc'](); case 'xcache': return $app['orm.cache.factory.xcache'](); case 'memcache': return $app['orm.cache.factory.memcache']($cacheOptions); case 'memcached': return $app['orm.cache.factory.memcached']($cacheOptions); case 'filesystem': return $app['orm.cache.factory.filesystem']($cacheOptions); case 'redis': return $app['orm.cache.factory.redis']($cacheOptions); default: throw new \RuntimeException("Unsupported cache type '{$driver}' specified"); } }); $app['orm.mapping_driver_chain.locator'] = $app->protect(function ($name = null) use($app) { $app['orm.ems.options.initializer'](); if (null === $name) { $name = $app['orm.ems.default']; } $cacheInstanceKey = 'orm.mapping_driver_chain.instances.' . $name; if (isset($app[$cacheInstanceKey])) { return $app[$cacheInstanceKey]; } return $app[$cacheInstanceKey] = $app['orm.mapping_driver_chain.factory']($name); }); $app['orm.mapping_driver_chain.factory'] = $app->protect(function ($name) use($app) { return new MappingDriverChain(); }); $app['orm.add_mapping_driver'] = $app->protect(function (MappingDriver $mappingDriver, $namespace, $name = null) use($app) { $app['orm.ems.options.initializer'](); if (null === $name) { $name = $app['orm.ems.default']; } $driverChain = $app['orm.mapping_driver_chain.locator']($name); $driverChain->addDriver($mappingDriver, $namespace); }); $app['orm.generate_psr0_mapping'] = $app->protect(function ($resourceMapping) use($app) { $mapping = array(); foreach ($resourceMapping as $resourceNamespace => $entityNamespace) { $directory = $app['psr0_resource_locator']->findFirstDirectory($resourceNamespace); if (!$directory) { throw new \InvalidArgumentException("Resources for mapping '{$entityNamespace}' could not be located; Looked for mapping resources at '{$resourceNamespace}'"); } $mapping[$directory] = $entityNamespace; } return $mapping; }); $app['orm.strategy.naming'] = $app->share(function ($app) { return new DefaultNamingStrategy(); }); $app['orm.strategy.quote'] = $app->share(function ($app) { return new DefaultQuoteStrategy(); }); $app['orm.entity_listener_resolver'] = $app->share(function ($app) { return new DefaultEntityListenerResolver(); }); $app['orm.repository_factory'] = $app->share(function ($app) { return new DefaultRepositoryFactory(); }); $app['orm.em'] = $app->share(function ($app) { $ems = $app['orm.ems']; return $ems[$app['orm.ems.default']]; }); $app['orm.em.config'] = $app->share(function ($app) { $configs = $app['orm.ems.config']; return $configs[$app['orm.ems.default']]; }); }
/** * @param Container $container */ public function register(Container $container) { foreach ($this->getMongodbOdmDefaults($container) as $key => $value) { if (!isset($container[$key])) { $container[$key] = $value; } } $container['mongodbodm.dm.default_options'] = array('connection' => 'default', 'database' => null, 'mappings' => array(), 'types' => array()); $container['mongodbodm.dms.options.initializer'] = $container->protect(function () use($container) { static $initialized = false; if ($initialized) { return; } $initialized = true; if (!isset($container['mongodbodm.dms.options'])) { $container['mongodbodm.dms.options'] = array('default' => isset($container['mongodbodm.dm.options']) ? $container['mongodbodm.dm.options'] : array()); } $tmp = $container['mongodbodm.dms.options']; foreach ($tmp as $name => &$options) { $options = array_replace($container['mongodbodm.dm.default_options'], $options); if (!isset($container['mongodbodm.dms.default'])) { $container['mongodbodm.dms.default'] = $name; } } $container['mongodbodm.dms.options'] = $tmp; }); $container['mongodbodm.dm_name_from_param_key'] = $container->protect(function ($paramKey) use($container) { $container['mongodbodm.dms.options.initializer'](); if (isset($container[$paramKey])) { return $container[$paramKey]; } return $container['mongodbodm.dms.default']; }); $container['mongodbodm.dms'] = function () use($container) { $container['mongodbodm.dms.options.initializer'](); $dms = new Container(); foreach ($container['mongodbodm.dms.options'] as $name => $options) { if ($container['mongodbodm.dms.default'] === $name) { // we use shortcuts here in case the default has been overridden $config = $container['mongodbodm.dm.config']; } else { $config = $container['mongodbodm.dms.config'][$name]; } if (isset($options['database'])) { $config->setDefaultDB($options['database']); } $dms[$name] = function () use($container, $options, $config) { return DocumentManager::create($container['mongodbs'][$options['connection']], $config, $container['mongodbs.event_manager'][$options['connection']]); }; } return $dms; }; $container['mongodbodm.dms.config'] = function () use($container) { $container['mongodbodm.dms.options.initializer'](); $configs = new Container(); foreach ($container['mongodbodm.dms.options'] as $name => $options) { $config = new Configuration(); $container['mongodbodm.cache.configurer']($name, $config, $options); $config->setProxyDir($container['mongodbodm.proxies_dir']); $config->setProxyNamespace($container['mongodbodm.proxies_namespace']); $config->setAutoGenerateProxyClasses($container['mongodbodm.auto_generate_proxies']); $config->setHydratorDir($container['mongodbodm.hydrator_dir']); $config->setHydratorNamespace($container['mongodbodm.hydrator_namespace']); $config->setAutoGenerateHydratorClasses($container['mongodbodm.auto_generate_hydrators']); $config->setClassMetadataFactoryName($container['mongodbodm.class_metadata_factory_name']); $config->setDefaultRepositoryClassName($container['mongodbodm.default_repository_class']); $config->setRepositoryFactory($container['mongodbodm.repository_factory']); $chain = $container['mongodbodm.mapping_driver_chain.locator']($name); foreach ((array) $options['mappings'] as $entity) { if (!is_array($entity)) { throw new \InvalidArgumentException("The 'mongodbodm.dm.options' option 'mappings' should be an array of arrays."); } if (isset($entity['alias'])) { $config->addDocumentNamespace($entity['alias'], $entity['namespace']); } switch ($entity['type']) { case 'annotation': $useSimpleAnnotationReader = isset($entity['use_simple_annotation_reader']) ? $entity['use_simple_annotation_reader'] : true; $driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader); $chain->addDriver($driver, $entity['namespace']); break; case 'yml': $driver = new YamlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_yml': $driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_xml': $driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'php': $driver = new StaticPHPDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } $config->setMetadataDriverImpl($chain); foreach ((array) $options['types'] as $typeName => $typeClass) { if (Type::hasType($typeName)) { Type::overrideType($typeName, $typeClass); } else { Type::addType($typeName, $typeClass); } } $configs[$name] = $config; } return $configs; }; $container['mongodbodm.cache.configurer'] = $container->protect(function ($name, Configuration $config, $options) use($container) { $config->setMetadataCacheImpl($container['mongodbodm.cache.locator']($name, 'metadata', $options)); }); $container['mongodbodm.cache.locator'] = $container->protect(function ($name, $cacheName, $options) use($container) { $cacheNameKey = $cacheName . '_cache'; if (!isset($options[$cacheNameKey])) { $options[$cacheNameKey] = $container['mongodbodm.default_cache']; } if (isset($options[$cacheNameKey]) && !is_array($options[$cacheNameKey])) { $options[$cacheNameKey] = array('driver' => $options[$cacheNameKey]); } if (!isset($options[$cacheNameKey]['driver'])) { throw new \RuntimeException("No driver specified for '{$cacheName}'"); } $driver = $options[$cacheNameKey]['driver']; $cacheInstanceKey = 'mongodbodm.cache.instances.' . $name . '.' . $cacheName; if (isset($container[$cacheInstanceKey])) { return $container[$cacheInstanceKey]; } $cache = $container['mongodbodm.cache.factory']($driver, $options[$cacheNameKey]); if (isset($options['cache_namespace']) && $cache instanceof CacheProvider) { $cache->setNamespace($options['cache_namespace']); } return $container[$cacheInstanceKey] = $cache; }); $container['mongodbodm.cache.factory.backing_memcache'] = $container->protect(function () { return new \Memcache(); }); $container['mongodbodm.cache.factory.memcache'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcache cache'); } /** @var \Memcache $memcache */ $memcache = $container['mongodbodm.cache.factory.backing_memcache'](); $memcache->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }); $container['mongodbodm.cache.factory.backing_memcached'] = $container->protect(function () { return new \Memcached(); }); $container['mongodbodm.cache.factory.memcached'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcached cache'); } /** @var \Memcached $memcached */ $memcached = $container['mongodbodm.cache.factory.backing_memcached'](); $memcached->addServer($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }); $container['mongodbodm.cache.factory.backing_redis'] = $container->protect(function () { return new \Redis(); }); $container['mongodbodm.cache.factory.redis'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for redis cache'); } /** @var \Redis $redis */ $redis = $container['mongodbodm.cache.factory.backing_redis'](); $redis->connect($cacheOptions['host'], $cacheOptions['port']); if (isset($cacheOptions['password'])) { $redis->auth($cacheOptions['password']); } $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }); $container['mongodbodm.cache.factory.array'] = $container->protect(function () { return new ArrayCache(); }); $container['mongodbodm.cache.factory.apc'] = $container->protect(function () { return new ApcCache(); }); $container['mongodbodm.cache.factory.apcu'] = $container->protect(function () { return new ApcuCache(); }); $container['mongodbodm.cache.factory.xcache'] = $container->protect(function () { return new XcacheCache(); }); $container['mongodbodm.cache.factory.filesystem'] = $container->protect(function ($cacheOptions) { if (empty($cacheOptions['path'])) { throw new \RuntimeException('FilesystemCache path not defined'); } $cacheOptions += array('extension' => FilesystemCache::EXTENSION, 'umask' => 02); return new FilesystemCache($cacheOptions['path'], $cacheOptions['extension'], $cacheOptions['umask']); }); $container['mongodbodm.cache.factory.couchbase'] = $container->protect(function ($cacheOptions) { if (empty($cacheOptions['host'])) { $cacheOptions['host'] = '127.0.0.1'; } if (empty($cacheOptions['bucket'])) { $cacheOptions['bucket'] = 'default'; } $couchbase = new \Couchbase($cacheOptions['host'], $cacheOptions['user'], $cacheOptions['password'], $cacheOptions['bucket']); $cache = new CouchbaseCache(); $cache->setCouchbase($couchbase); return $cache; }); $container['mongodbodm.cache.factory'] = $container->protect(function ($driver, $cacheOptions) use($container) { switch ($driver) { case 'array': return $container['mongodbodm.cache.factory.array'](); case 'apc': return $container['mongodbodm.cache.factory.apc'](); case 'apcu': return $container['mongodbodm.cache.factory.apcu'](); case 'xcache': return $container['mongodbodm.cache.factory.xcache'](); case 'memcache': return $container['mongodbodm.cache.factory.memcache']($cacheOptions); case 'memcached': return $container['mongodbodm.cache.factory.memcached']($cacheOptions); case 'filesystem': return $container['mongodbodm.cache.factory.filesystem']($cacheOptions); case 'redis': return $container['mongodbodm.cache.factory.redis']($cacheOptions); case 'couchbase': return $container['mongodbodm.cache.factory.couchbase']($cacheOptions); default: throw new \RuntimeException("Unsupported cache type '{$driver}' specified"); } }); $container['mongodbodm.mapping_driver_chain.locator'] = $container->protect(function ($name = null) use($container) { $container['mongodbodm.dms.options.initializer'](); if (null === $name) { $name = $container['mongodbodm.dms.default']; } $cacheInstanceKey = 'mongodbodm.mapping_driver_chain.instances.' . $name; if (isset($container[$cacheInstanceKey])) { return $container[$cacheInstanceKey]; } return $container[$cacheInstanceKey] = $container['mongodbodm.mapping_driver_chain.factory']($name); }); $container['mongodbodm.mapping_driver_chain.factory'] = $container->protect(function ($name) use($container) { return new MappingDriverChain(); }); $container['mongodbodm.add_mapping_driver'] = $container->protect(function (MappingDriver $mappingDriver, $namespace, $name = null) use($container) { $container['mongodbodm.dms.options.initializer'](); if (null === $name) { $name = $container['mongodbodm.dms.default']; } /** @var MappingDriverChain $driverChain */ $driverChain = $container['mongodbodm.mapping_driver_chain.locator']($name); $driverChain->addDriver($mappingDriver, $namespace); }); $container['mongodbodm.repository_factory'] = function ($container) { return new DefaultRepositoryFactory(); }; $container['mongodbodm.dm'] = function ($container) { $dms = $container['mongodbodm.dms']; return $dms[$container['mongodbodm.dms.default']]; }; $container['mongodbodm.dm.config'] = function ($container) { $configs = $container['mongodbodm.dms.config']; return $configs[$container['mongodbodm.dms.default']]; }; }
protected function _getCacheDriver() { $driver = new RedisCache(); $driver->setRedis($this->_redis); return $driver; }
<?php require_once 'vendor/autoload.php'; use Doctrine\Common\Cache\RedisCache; use Doctrine\DBAL\Logging\EchoSQLLogger; use Doctrine\ORM\Cache\DefaultCacheFactory; use Doctrine\ORM\Cache\RegionsConfiguration; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/src'], $isDevMode); $config->setSQLLogger(new EchoSQLLogger()); $redis = new Redis(); $redis->connect('localhost'); $cache = new RedisCache(); $cache->setRedis($redis); $cacheFactory = new DefaultCacheFactory(new RegionsConfiguration(), $cache); $config->setSecondLevelCacheEnabled(); $config->getSecondLevelCacheConfiguration()->setCacheFactory($cacheFactory); $conn = ['driver' => 'pdo_sqlite', 'path' => __DIR__ . '/db.sqlite']; $entityManager = EntityManager::create($conn, $config); return $entityManager;