/** * * 获取缓存信息 * @return \Doctrine\Common\Cache\MongoDBCache */ protected function getCache() { $namespace = 'PanGuKTD_ORM_'; $cacheConfig = config('doctrine'); $cacheName = $cacheConfig['name']; $cache = null; if ($cacheName == 'array') { $cache = new \Doctrine\Common\Cache\ArrayCache(); } elseif ($cacheName == 'xcache') { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif ($cacheName == 'memcached') { $memcached = new \Memcached(); $memcached->addServers($cacheConfig['memcached']); $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($memcached); $cache->setNamespace($namespace); } elseif ($cacheName == 'memcache') { $memcache = new \Memcache(); foreach ($cacheConfig['memcache'] as $key => $value) { $memcache->addServer($value['host'], $value['port'], $value['persistent'], $value['weight']); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); $cache->setNamespace($namespace); } elseif ($cacheName == 'apc') { $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($namespace); } elseif ($cacheName == 'mongo') { $host = $cacheConfig['mongo']['host']; $port = $cacheConfig['mongo']['port']; $opt = $cacheConfig['mongo']['options']; $mongo = new \MongoClient("mongodb://{$host}:{$port}", $opt); $mongo = new \MongoDB($mongo, 'doctrine_orm_cache'); $conn = new \MongoCollection($mongo, $namespace); $cache = new \Doctrine\Common\Cache\MongoDBCache($conn); $cache->setNamespace($namespace); } elseif ($cacheName == 'redis') { $host = $cacheConfig['redis']['host']; $port = $cacheConfig['redis']['port']; $redis = new \Redis(); $redis->connect($host, $port); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } return $cache; }
/** * Detects the correct doctrine cache driver for the user caching engine in use * @return \Doctrine\Common\Cache\AbstractCache The doctrine cache driver object */ public function getDoctrineCacheDriver() { if ($this->doctrineCacheEngine) { // return cache engine if already set return $this->doctrineCacheEngine; } $userCacheEngine = $this->getUserCacheEngine(); // check if user caching is active if (!$this->getUserCacheActive()) { $userCacheEngine = \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_OFF; } switch ($userCacheEngine) { case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_APC: $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHE: $memcache = $this->getMemcache(); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHED: $memcached = $this->getMemcached(); $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($memcached); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_XCACHE: $cache = new \Doctrine\Common\Cache\XcacheCache(); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_FILESYSTEM: $cache = new \Cx\Core_Modules\Cache\Controller\Doctrine\CacheDriver\FileSystemCache($this->strCachePath); break; default: $cache = new \Doctrine\Common\Cache\ArrayCache(); break; } // set the doctrine cache engine to avoid getting it a second time $this->doctrineCacheEngine = $cache; return $cache; }