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;
     });
 }
예제 #2
0
 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;
 }
예제 #3
0
 /**
  * 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.');
 }
예제 #4
0
 /**
  * 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);
 }
예제 #5
0
 /**
  * @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);
 }