Example #1
0
func redisOpener(url *config.URL) (driver.Driver, error) {
	password := url.Fragment.Get("password")
	db := -1
	maxIdle := DefaultMaxIdle
	maxActive := DefaultMaxActive
	idleTimeout := DefaultIdleTimeout
	if d := url.Fragment.Get("db"); d != "" {
		val, ok := url.Fragment.Int("db")
		if !ok {
			return nil, fmt.Errorf("invalid db %q, must be an integer", d)
		}
		db = val
	}
	if v, ok := url.Fragment.Int("max_idle"); ok {
		maxIdle = v
	}
	if v, ok := url.Fragment.Int("max_active"); ok {
		maxActive = v
	}
	if v, ok := url.Fragment.Int("idle_timeout"); ok {
		idleTimeout = v
	}
	server := driver.DefaultPort(url.Value, 6379)
	pool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			}
			if db != -1 {
				if _, err := c.Do("SELECT", db); err != nil {
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
		MaxIdle:     maxIdle,
		MaxActive:   maxActive,
		IdleTimeout: time.Duration(idleTimeout) * time.Second,
	}
	return &redisDriver{pool: pool}, nil
}
Example #2
0
func memcacheOpener(url *config.URL) (driver.Driver, error) {
	hosts := strings.Split(url.Value, ",")
	conns := make([]string, len(hosts))
	for ii, v := range hosts {
		conns[ii] = driver.DefaultPort(v, 11211)
	}
	client, err := memcache.New(conns...)
	if err != nil {
		return nil, err
	}
	if tm, ok := url.Fragment.Int("timeout"); ok {
		client.SetTimeout(time.Millisecond * time.Duration(tm))
	}
	if maxIdle, ok := url.Fragment.Int("max_idle"); ok {
		client.SetMaxIdleConnsPerAddr(maxIdle)
	}
	return &memcacheDriver{Client: client}, nil
}