예제 #1
0
파일: Cache.php 프로젝트: Toushi/flow
 /**
  * Loads the configured driver and validates it.
  *
  * @param   array|string  custom configuration or config group name
  * @return  void
  */
 public function __construct($config = FALSE)
 {
     if (is_string($config)) {
         $name = $config;
         // Test the config group name
         if (($config = Kohana::config('cache.' . $config)) === NULL) {
             throw new Kohana_Exception('cache.undefined_group', $name);
         }
     }
     if (is_array($config)) {
         // Append the default configuration options
         $config += Kohana::config('cache.default');
     } else {
         // Load the default group
         $config = Kohana::config('cache.default');
     }
     // Cache the config in the object
     $this->config = $config;
     // Set driver name
     $driver = 'Cache_' . ucfirst($this->config['driver']) . '_Driver';
     // Load the driver
     if (!Kohana::auto_load($driver)) {
         throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
     }
     // Initialize the driver
     $this->driver = new $driver($this->config['params']);
     // Validate the driver
     if (!$this->driver instanceof Cache_Driver) {
         throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
     }
     Kohana::log('debug', 'Cache Library initialized');
     if (self::$loaded !== TRUE) {
         $this->config['requests'] = (int) $this->config['requests'];
         if ($this->config['requests'] > 0 and mt_rand(1, $this->config['requests']) === 1) {
             // Do garbage collection
             $this->driver->delete_expired();
             Kohana::log('debug', 'Cache: Expired caches deleted.');
         }
         // Cache has been loaded once
         self::$loaded = TRUE;
     }
 }