コード例 #1
0
 /**
  * Register Cache Component
  * @throws BootstrapException
  */
 protected function registerCache()
 {
     // Container has Cache component, must be defined in config
     if (!property_exists($this->config->app, "cache")) {
         throw BootstrapException::cacheNode();
     }
     $cacheConfig = $this->config->app->cache;
     // Cache configuration
     // Make sure STATUS is set and is bool
     if (!property_exists($cacheConfig, "status") || !is_bool($cacheConfig->status)) {
         throw BootstrapException::cacheStatus();
     }
     // Not use cache?
     if ($this->config->app->cache->status !== true) {
         return;
         // Return
     }
     // Which cache engine to use?
     if (!property_exists($cacheConfig, "engine") || !is_string($cacheConfig->engine)) {
         throw BootstrapException::cacheEngine("");
     }
     switch (strtolower($cacheConfig->engine)) {
         case "redis":
             $cacheEngine = Cache::ENGINE_REDIS;
             break;
         case "memcached":
             $cacheEngine = Cache::ENGINE_MEMCACHED;
             break;
         default:
             throw BootstrapException::cacheEngine($cacheConfig->engine);
     }
     $cacheHost = $cacheConfig->host ?? "127.0.0.1";
     $cachePort = intval($cacheConfig->port ?? 8000);
     $this->cache = $this->container->get("Cache");
     $this->cache->addServer($cacheHost, $cachePort, 1, $cacheEngine);
     try {
         $this->cache->connect();
     } catch (\ComelyException $e) {
         if (property_exists($cacheConfig, "terminate")) {
             if ($cacheConfig->terminate === true) {
                 throw new BootstrapException(__METHOD__, $e->getMessage(), $e->getCode());
             }
         }
         // Trigger E_USER_WARNING
         trigger_error(sprintf('Failed to connect with %1$s server on %2$s:%3$d: %4$s', strtoupper($cacheConfig->engine), $cacheHost, $cachePort, $e->getMessage()), E_USER_WARNING);
     }
 }