/** * 创建ElexConfig的实例 * @param $config_file * @param $options * @return ElexConfig */ public static function getInstance($config_file, $options = null) { if (!self::$instance instanceof self) { self::$use_apc_cache = function_exists('apc_fetch'); // 验证配置文件路径的正确性 self::checkConfigPath($config_file); // 先尝试从缓存中反序列化 self::$instance = self::getConfigFromCache($config_file); if (empty(self::$instance)) { // 创建一个新的ElexConfig对象 self::$instance = new self($config_file, $options); // 将对象序列化后写入缓存 self::writeConfigToCache($config_file, self::$instance); } } return self::$instance; }
protected function init($options) { parent::init($options); // 处理包含"|"的section,将其扩展为多个section if (is_array($this->config) && !empty($this->config)) { foreach ($this->config as $section => $values) { if (strpos($section, '|') !== false) { $sections = explode('|', $section); foreach ($sections as $sec) { $this->config[trim($sec)] = $values; } unset($this->config[$section]); } } } // 针对fields进行特殊处理,因为fields字段属于比较费时间来进行解析的字段,所以最好将其缓存到APC缓存中去 $fields_defs_as_str = $this->config[self::TABLE_FIELDS]; if (isset($fields_defs_as_str)) { $fields_defs = array(); foreach ($fields_defs_as_str as $table_name => $fields_def_as_str) { $fields_def = preg_split('/[:+]/', $fields_def_as_str); for ($i = 0, $length = count($fields_def) - 1; $i < $length; $i = $i + 3) { $field_name = $fields_def[$i]; $field_type = $fields_def[$i + 1]; $default_value = $fields_def[$i + 2]; if (!in_array($field_type, array(FieldType::TYPE_INTEGER, FieldType::TYPE_STRING, FieldType::TYPE_FLOAT, FieldType::TYPE_PACKED))) { $this->throwException("fields definition error:unknown type[" . $fields_def[$i + 1] . "]", ELEX_ERR_CONFIG_ERROR); } $field_props = array("name" => $field_name, "type" => $field_type); if (strpos($default_value, "function<>") !== false) { $arr = explode("<>", $default_value); $field_props['default'] = "function"; $field_props['function'] = $arr[1]; } else { $field_props['default'] = $default_value; } $fields_defs[$table_name][$field_props['name']] = $field_props; } } $this->config[self::TABLE_FIELDS] = $fields_defs; } // 缓存提交数据库阈值cache_commit_threshold $cache_commit_thresholds_as_str = $this->config[self::TABLE_CACHE_COMMIT_THRESHOLD]; if (isset($cache_commit_thresholds_as_str)) { $cache_commit_threshold_defs = array(); foreach ($cache_commit_thresholds_as_str as $table_name => $cache_commit_threshold_as_str) { $parts = explode(":", $cache_commit_threshold_as_str); $cache_commit_threshold_defs[$table_name]['threshold'] = isset($parts[0]) ? intval($parts[0]) : 0; $cache_commit_threshold_defs[$table_name]['expire'] = isset($parts[1]) ? intval($parts[1]) : 1800; } $this->config[self::TABLE_CACHE_COMMIT_THRESHOLD] = $cache_commit_threshold_defs; } // 压缩数据处理pack_fields $pack_fields_as_str = $this->config[self::TABLE_PACK_FIELDS]; if (isset($pack_fields_as_str)) { $pack_fields_defs = array(); foreach ($pack_fields_as_str as $table_name => $pack_field_as_str) { $packed_fields_def = array(); $parts = explode(",", $pack_field_as_str); foreach ($parts as $part) { $preg_parts = preg_split('/[:\\/]/', $part); $sub_fields = array(); $unit_size = 0; $format = substr($part, strpos($part, ":") + 1); for ($i = 1; $i < count($preg_parts); $i++) { $preg_part = $preg_parts[$i]; $sub_field_name = substr($preg_part, 1); $sub_fields[$sub_field_name] = $preg_part[0]; switch ($preg_part[0]) { case "C": $unit_size += 1; break; case "S": $unit_size += 2; break; case "L": $unit_size += 4; break; } } $packed_fields_def[$preg_parts[0]] = array("unit_size" => $unit_size, "sub_fields" => $sub_fields, "format" => $format); } $pack_fields_defs[$table_name] = $packed_fields_def; } $this->config[self::TABLE_PACK_FIELDS] = $pack_fields_defs; } $global_config = $this->config[self::CONFIG_GLOBAL]; $this->service_is_closed = $this->getValue($global_config, self::SERVICE_CLOSED, false, 'bool'); $this->debug_mode = $this->getValue($global_config, self::DEBUG_MODE, false, 'bool'); $this->perf_test_mode = $this->getValue($global_config, self::PERF_TEST_MODE, false, 'bool'); }