Example #1
0
func RedisInit(server string, pwd string, params map[string]string) {
	var max_idle, max_active int
	var idle_timeout time.Duration

	if params["maxIdle"] != "" {
		max_idle = int(n_utils.Be_int(params["maxIdle"]))
	} else {
		max_idle = 3
	}

	// 未设置则没限制
	if params["maxActive"] != "" {
		max_active = int(n_utils.Be_int(params["maxActive"]))
	} else {
		max_active = 0
	}

	if params["idleTimeout"] != "" {
		idle_timeout = time.Duration(n_utils.Be_int(params["idleTimeout"]))
	} else {
		idle_timeout = 240
	}

	Pool = &redis.Pool{
		MaxIdle:     max_idle,
		MaxActive:   max_active,
		IdleTimeout: idle_timeout * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}

			if pwd != "" {
				if _, err := c.Do("AUTH", pwd); err != nil {
					c.Close()
					return nil, err
				}
			}

			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}
Example #2
0
func (this *DefaultResult) get(key string, v interface{}) (err error) {
	if err = this.CheckStatus(); err != nil {
		log.Warn(err)
		return
	}

	switch v.(type) {
	case *string:
		data_map := this.Data.(map[string]interface{})
		var s *string = v.(*string)
		*s = n_utils.Be_string(data_map[key])
		v = s
		return
	case *int64:
		data_map := this.Data.(map[string]interface{})
		var i *int64 = v.(*int64)
		*i = n_utils.Be_int(data_map[key])
		v = i
		return
	default:
		this.Data = v
		return json.Unmarshal(this.GetRawData(), this)
	}
	return
}