/** * @param array $cacheConfig * @return Cache\CacheProvider */ protected function resolveCacheAdapter(array $cacheConfig) { switch ($cacheConfig['adapter']) { case Cache\ArrayCache::class: case Cache\ApcuCache::class: return new $cacheConfig['adapter'](); case Cache\FilesystemCache::class: case Cache\PhpFileCache::class: return new $cacheConfig['adapter']($cacheConfig['options']['dir']); case Cache\MemcachedCache::class: $memcached = new \Memcached(); $servers = isset($cacheConfig['options']['servers']) ? $cacheConfig['options']['servers'] : []; foreach ($servers as $server) { if (!isset($server['host'])) { continue; } $port = isset($server['port']) ? intval($server['port']) : 11211; $memcached->addServer($server['host'], $port); } $cache = new Cache\MemcachedCache(); $cache->setMemcached($memcached); return $cache; default: return new Cache\ArrayCache(); } }
public function register(Application $app) { $app['cache.memcached'] = $app->share(function () use($app) { if (!class_exists('\\Doctrine\\Common\\Cache\\MemcachedCache')) { throw new \Exception('You need to include doctrine/common in order to use the cache service'); } $cache = new MemcachedCache(); $cache->setMemcached($app['memcached']); return $cache; }); $app['cache.predis'] = $app->share(function () use($app) { if (!class_exists('\\Doctrine\\Common\\Cache\\PredisCache')) { throw new \Exception('You need to include doctrine/common in order to use the cache service'); } return new PredisCache($app['predis.client']); }); $app['cache.predis_serializer'] = $app->share(function () use($app) { if (!class_exists('\\Doctrine\\Common\\Cache\\PredisCache')) { throw new \Exception('You need to include doctrine/common in order to use the cache service'); } return new SerializingPredisCache($app['predis.client']); }); $app['cache'] = $app->share(function () use($app) { if ($app->offsetExists('cache.default')) { return $app[$app['cache.default']]; } return $app['memcached']; }); }
/** * @test */ public function WithMemcachedType_Build_ReturnMemcached() { $memcachedCache = new MemcachedCache(); $memcachedCache->setMemcached(new MemcachedSpy()); $cache = $this->cacheBuilder->withCacheProvider($memcachedCache)->build(); $this->assertAttributeInstanceOf('Doctrine\\Common\\Cache\\MemcachedCache', 'cache', $cache); }
public function register(Application $app) { $app->setParameter('cache', ['namespace' => null, 'type' => 'array']); $app->singleton('cache', function () use($app) { $cache = null; $type = $app->getParameter('cache.type', 'array'); if ($type == 'array') { $cache = new ArrayCache(); } elseif ($type == 'apc') { $cache = new ApcCache(); } elseif ($type == 'xcache') { $cache = new XcacheCache(); } elseif ($type == 'memcache') { $cache = new MemcacheCache(); $memcache = new \Memcache(); $memcache->addserver($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcache($memcache); } elseif ($type == 'memcached') { $cache = new MemcachedCache(); $memcached = new \Memcached(); $memcached->addServer($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcached($memcached); } $cache->setNamespace($app->getParameter('cache.namespace')); return $cache; }); }
/** * Register Guzzle with Silex * * @param Application $app Application to register with */ public function register(Application $app) { parent::register($app); $app['guzzle'] = $app->share($app->extend('guzzle', function (ServiceBuilder $builder, $app) { foreach ($app['guzzle.plugins'] as $plugin) { $builder->addGlobalPlugin($plugin); } return $builder; })); $app['guzzle.request_logger_plugin'] = $app->share(function () use($app) { return new RequestLoggerPlugin($app['logger']); }); $app['guzzle.request_before_send_logger_plugin'] = $app->share(function () use($app) { return new RequestBeforeSendLoggerPlugin($app['logger']); }); $app['guzzle.request_token_plugin'] = $app->share(function () use($app) { return new RequestTokenPlugin($app['request_token'], $app['request_stack']); }); $app['guzzle.log_plugin'] = $app->share(function () use($app) { $logAdapter = new MonologGuzzleLogAdapter($app['logger']); $logFormatter = new MessageFormatter('{code} {method} {url} in {total_time}s'); return new LogPlugin($logAdapter, $logFormatter); }); $app['guzzle.cache_plugin'] = $app->share(function () use($app) { $cache = new MemcachedCache(); $cache->setMemcached($app['memcached']); return new CachePlugin(array('storage' => new DefaultCacheStorage(new DoctrineCacheAdapter($cache), '', $app['cache.default_ttl']))); }); }
/** * @inheritdoc */ protected function createStorage() { $m = new \Memcached(); $m->addServer("localhost", 11211); $c = new MemcachedCache(); $c->setMemcached($m); return new CacheStorage($c); }
/** * Sets up and returns the CacheProvider * @param $config * @return \Doctrine\Common\Cache\CacheProvider */ protected function initialize($config) { $memcached = new \Memcached(); $memcached->addserver($config['host'], $config['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }
/** * {@inheritdoc} */ public function getAdapter(array $config) { $memcached = new Memcached(); $memcached->addServer($config['host'], $config['port']); $client = new MemcachedCache(); $client->setMemcached($memcached); return new DoctrineCachePool($client); }
/** * @param array $servers Array of servers, where each server entry is in format array(SERVER_IP, SERVER_PORT, SERVER_WEIGHT), where SERVER_WEIGHT is optional (the bigger value, the bigger there is chance to be connected to that server). * @param string $prefix Prefix for all the keys stored in memcached using this adapter. */ public function __construct(array $servers, $prefix) { $driver = new MemcachedCache(); $memcached = new \Memcached(); $memcached->addServers($servers); $memcached->setOption(\Memcached::OPT_PREFIX_KEY, $prefix); $driver->setMemcached($memcached); $this->driver = $driver; parent::__construct($driver); }
protected function createMemcachedDriver($name) { $host = $this->config->get("neptune.cache.{$name}.host", '127.0.0.1'); $port = $this->config->get("neptune.cache.{$name}.port", '11211'); $memcached = new Memcached(); $memcached->addserver($host, $port); $driver = new MemcachedCache(); $driver->setNamespace($this->config->getRequired("neptune.cache.{$name}.namespace")); $driver->setMemcached($memcached); return $driver; }
public function make($config = null) { if (!extension_loaded('memcached')) { throw new \RuntimeException('Memcached extension was not loaded.'); } $memcached = new \Memcached(); $memcached->connect($config['host'], $config['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }
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); }
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 MemcachedCache */ public function resolve() { $cache = new MemcachedCache(); if (extension_loaded('memcached')) { $memcached = new Memcached(); foreach ($this->config['servers'] as $server) { $memcached->addServer($server['host'], $server['port'], $server['weight']); } $cache->setMemcached($memcached); return $cache; } throw new DriverNotFound('Memcached extension not loaded'); }
private function initializeEntityManager() { if ($this->configuration->get('cache')) { $memcached = $this->container->get('memcached'); $cache = new MemcachedCache(); $cache->setMemcached($memcached); } else { $cache = null; } $config = Setup::createAnnotationMetadataConfiguration([ROOT_DIR . "/src"], true, ROOT_DIR . '/cache/doctrine/proxy', $cache); $connection = ['driver' => $this->configuration->get('driver'), 'user' => $this->configuration->get('user'), 'password' => $this->configuration->get('password'), 'dbname' => $this->configuration->get('dbname'), 'host' => $this->configuration->get('host')]; $this->entityManager = EntityManager::create($connection, $config); $eventManager = $this->entityManager->getEventManager(); $eventManager->addEventSubscriber(new DoctrineSubscriber()); }
/** * 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.'); }
public function register(Application $app) { $app['memcached'] = $app->share(function () use($app) { $memcached = new \Memcached($app['memcached.identifier']); $memcached->setOption(\Memcached::OPT_COMPRESSION, false); $memcached->setOption(\Memcached::OPT_PREFIX_KEY, $app['memcached.prefix']); $serversToAdd = array_udiff($app['memcached.servers'], $memcached->getServerList(), function ($a, $b) { return $a['host'] == $b['host'] && $a['port'] == $b['port'] ? 0 : 1; }); if (count($serversToAdd)) { $memcached->addServers($serversToAdd); } return $memcached; }); $app['cache'] = $app->share(function () use($app) { if (!class_exists('\\Doctrine\\Common\\Cache\\MemcachedCache')) { throw new \Exception('You need to include doctrine/common in order to use the cache service'); } $cache = new MemcachedCache(); $cache->setMemcached($app['memcached']); return $cache; }); }
public function setMemcached(Memcached $memcached) { parent::setMemcached($memcached); }
/** * Conexion::memcached() * * Genera el proceso de conexion a servidor memcached * * @param object $confg * @return void */ private function memcached() { $parametros = ConfigCache::leer('driver', 'memcached'); $OPT_BINARY_PROTOCOL = (is_bool($parametros['usuario']) == false and is_bool($parametros['clave']) == false) ? (bool) true : (bool) false; $memcached = new \Memcached(); $memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, $OPT_BINARY_PROTOCOL); $memcached->addServer($parametros['servidor'], $parametros['puerto']); if (is_bool($parametros['usuario']) == false and is_bool($parametros['clave']) == false) { $memcached->setSaslAuthData($parametros['usuario'], $parametros['clave']); } $cacheDriver = new \Doctrine\Common\Cache\MemcachedCache(); $cacheDriver->setMemcached($memcached); return $cacheDriver; }
/** * {@inheritDoc} */ protected function _getCacheDriver() { $driver = new MemcachedCache(); $driver->setMemcached($this->memcached); return $driver; }
/** * 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; }
/** * @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']]; }; }
/** * Creates the cache providers * * @param array $config Array of cache configs */ private function createCache(array $config) { switch ($config['type']) { case 'redis': if (!class_exists('\\Redis')) { throw new \LogicException("Redis is required."); } $redis = new Redis(); $config = array_merge(array('servers' => array(array('host' => 'localhost', 'port' => 6379, 'timeout' => 0)), 'persistent' => false, 'password' => null, 'dbindex' => null), $config); $connect = 'connect'; if ($config['persistent']) { $connect = 'pconnect'; } foreach ($config['servers'] as $server) { $redis->{$connect}($server['host'], $server['port'], $server['timeout']); } if (null !== $config['password']) { $redis->auth($config['password']); } if (null !== $config['dbindex']) { $redis->select($config['dbindex']); } $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); $cache = new RedisCache(); $cache->setRedis($redis); break; case 'memcached': if (!class_exists('\\Memcached')) { throw new \LogicException("Memcached is required."); } $config = array_merge(array('servers' => array('host' => 'localhost', 'port' => 11211, 'weight' => 100), 'persistent' => false, 'options' => null), $config); $memcached = new Memcached($config['persistent'] ? serialize($config['servers']) : null); foreach ($config['servers'] as $server) { $memcached->addServer($server['host'], $server['port'], isset($server['weight']) ? $server['weight'] : null); } if (null !== $config['options']) { $memcached->setOptions($config['options']); } $cache = new MemcachedCache(); $cache->setMemcached($memcached); break; default: throw new \InvalidArgumentException(sprintf("%s is not a valid cache type. ", $config['type'])); } $plugin = new CachePlugin(array('storage' => new DefaultCacheStorage(new DoctrineCacheAdapter($cache)))); $this->addSubscriber($plugin); }
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']]; }); }
/** * creates memcached cache * * @param array $config * @return MemcachedCache */ private function getMemcachedCache(array $config) { $host = 'localhost'; $port = 11211; if (isset($config['host'])) { $host = $config['host']; } if (isset($config['port'])) { $port = $config['port']; } $cache = new MemcachedCache(); $memcache = new \Memcached(); $memcache->addServer($host, $port); $cache->setMemcached($memcache); return $cache; }
/** * Get our Memcached instance * @since Version 3.9.1 * @return \Doctrine\Common\Cache\MemcachedCache */ public static function getMemcached() { if (!extension_loaded("memcached") || defined("PHPUNIT_RAILPAGE_TESTSUITE")) { return new NullCacheDriver(); } $Registry = Registry::getInstance(); $Config = self::getConfig(); try { $cacheDriver = $Registry->get("memcached"); } catch (Exception $e) { $Memcached = new PhpMemcached(); $Memcached->addServer($Config->Memcached->Host, 11211); $cacheDriver = new MemcachedCache(); $cacheDriver->setMemcached($Memcached); $Registry->set("memcached", $cacheDriver); } return $cacheDriver; }
/** * @param $EntityPath * @param $EntityNamespace * * @return Manager * @throws ORMException */ public function getEntityManager($EntityPath, $EntityNamespace) { // Sanitize Namespace $EntityNamespace = trim(str_replace(array('/', '\\'), '\\', $EntityNamespace), '\\') . '\\'; $MetadataConfiguration = Setup::createAnnotationMetadataConfiguration(array($EntityPath)); $MetadataConfiguration->setDefaultRepositoryClassName('\\SPHERE\\System\\Database\\Fitting\\Repository'); $MetadataConfiguration->addCustomHydrationMode('COLUMN_HYDRATOR', '\\SPHERE\\System\\Database\\Fitting\\ColumnHydrator'); $ConnectionConfig = $this->getConnection()->getConnection()->getConfiguration(); if (self::$ConditionMemcached || (self::$ConditionMemcached = class_exists('\\Memcached', false))) { /** @var Memcached $CacheDriver */ $CacheDriver = (new Cache(new Memcached()))->getCache(); $Cache = new MemcachedCache(); $Cache->setMemcached($CacheDriver->getServer()); $Cache->setNamespace($EntityPath); $ConnectionConfig->setResultCacheImpl($Cache); $MetadataConfiguration->setQueryCacheImpl($Cache); $MetadataConfiguration->setHydrationCacheImpl($Cache); if (self::$ConditionApc || (self::$ConditionApc = function_exists('apc_fetch'))) { $MetadataConfiguration->setMetadataCacheImpl(new ApcCache()); } else { $MetadataConfiguration->setMetadataCacheImpl(new ArrayCache()); } } else { if (self::$ConditionApc || (self::$ConditionApc = function_exists('apc_fetch'))) { $MetadataConfiguration->setMetadataCacheImpl(new ApcCache()); } else { $MetadataConfiguration->setMetadataCacheImpl(new ArrayCache()); } $MetadataConfiguration->setQueryCacheImpl(new ArrayCache()); $MetadataConfiguration->setHydrationCacheImpl(new ArrayCache()); $ConnectionConfig->setResultCacheImpl(new ArrayCache()); } // $ConnectionConfig->setSQLLogger( new Logger() ); return new Manager(EntityManager::create($this->getConnection()->getConnection(), $MetadataConfiguration), $EntityNamespace); }
/** * EntityManager * * @param string $EntityPath * @param string $EntityNamespace * * @return Manager */ public function getEntityManager($EntityPath, $EntityNamespace) { // Sanitize Namespace $EntityNamespace = trim(str_replace(array('/', '\\'), '\\', $EntityNamespace), '\\') . '\\'; // System Cache /** @var Memcached $SystemMemcached */ $SystemMemcached = (new Cache(new Memcached(), true))->getCache(); /** @var Apcu $SystemApc */ $SystemApc = (new Cache(new Apcu(), true))->getCache(); $MetadataConfiguration = Setup::createAnnotationMetadataConfiguration(array($EntityPath)); $MetadataConfiguration->setDefaultRepositoryClassName('\\SPHERE\\System\\Database\\Fitting\\Repository'); $MetadataConfiguration->addCustomHydrationMode('COLUMN_HYDRATOR', '\\SPHERE\\System\\Database\\Fitting\\ColumnHydrator'); $ConnectionConfig = $this->getConnection()->getConnection()->getConfiguration(); if ($this->UseCache) { if ($SystemMemcached->isAvailable()) { $Cache = new MemcachedCache(); $Cache->setMemcached($SystemMemcached->getServer()); $Cache->setNamespace($EntityPath); $ConnectionConfig->setResultCacheImpl($Cache); $MetadataConfiguration->setHydrationCacheImpl($Cache); if ($SystemApc->isAvailable()) { $MetadataConfiguration->setMetadataCacheImpl(new ApcCache()); $MetadataConfiguration->setQueryCacheImpl(new ApcCache()); } else { $MetadataConfiguration->setMetadataCacheImpl(new ArrayCache()); $MetadataConfiguration->setQueryCacheImpl(new ArrayCache()); } } else { if ($SystemApc->isAvailable()) { $MetadataConfiguration->setMetadataCacheImpl(new ApcCache()); } else { $MetadataConfiguration->setMetadataCacheImpl(new ArrayCache()); } $MetadataConfiguration->setQueryCacheImpl(new ArrayCache()); $MetadataConfiguration->setHydrationCacheImpl(new ArrayCache()); $ConnectionConfig->setResultCacheImpl(new ArrayCache()); } } if ($this->getDebugger()->isActive()) { $ConnectionConfig->setSQLLogger(new Logger()); } return new Manager(EntityManager::create($this->getConnection()->getConnection(), $MetadataConfiguration), $EntityNamespace); }
/** * Get memcached driver * * @return MemcachedCache|null */ private function getMemcached() { if (isset($this->configuration['memcached']) && is_array($this->configuration['memcached'])) { $memcached = new \Memcached(); foreach ($this->configuration['memcached'] as $host => $port) { $memcached->addServer($host, $port); } $cacheDriver = new MemcachedCache(); $cacheDriver->setMemcached($memcached); return $cacheDriver; } return null; }
protected function registerMemcachedCache(Config $config) { $config = $config->get('cache.drive.memcached.config'); $memcached = new \Memcached(); $memcached->addServer($config['host'], $config['port'], $config['weight']); $memcachedCache = new MemcachedCache(); $memcachedCache->setMemcached($memcached); $this->cacheDrive = $memcachedCache; }