コード例 #1
0
ファイル: redis.go プロジェクト: hillzhang/log_collection
// until redigo supports sharding/clustering, only one host will be in hostList
func NewRedisCache(host, psw string, db, idle, max int, toc, tor, tow, defaultExpiration time.Duration) RedisCache {
	var pool = &redis.Pool{
		MaxIdle:     idle,
		MaxActive:   max,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.DialTimeout("tcp", host, toc, tor, tow)
			if err != nil {
				return nil, err
			}
			if _, err := c.Do("AUTH", psw); err != nil {
				c.Close()
				return nil, err
			}
			c.Do("SELECT", db)
			// check with PING
			if _, err := c.Do("PING"); err != nil {
				c.Close()
				return nil, err
			}
			return c, err
		},
		// custom connection test method
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if _, err := c.Do("PING"); err != nil {
				return err
			}
			return nil
		},
	}
	return RedisCache{pool, defaultExpiration}
}
コード例 #2
0
ファイル: testdb.go プロジェクト: hillzhang/log_collection
// Dial dials the local Redis server and selects database 9. To prevent
// stomping on real data, DialTestDB fails if database 9 contains data. The
// returned connection flushes database 9 on close.
func Dial() (redis.Conn, error) {
	c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
	if err != nil {
		return nil, err
	}

	_, err = c.Do("SELECT", "9")
	if err != nil {
		c.Close()
		return nil, err
	}

	n, err := redis.Int(c.Do("DBSIZE"))
	if err != nil {
		c.Close()
		return nil, err
	}

	if n != 0 {
		c.Close()
		return nil, errors.New("database #9 is not empty, test can not continue")
	}

	return testConn{c}, nil
}