func init() { db, _ := beego.AppConfig.Int("ipdb") port, _ := beego.AppConfig.Int("ipport") spec := redis.DefaultSpec().Host(beego.AppConfig.String("iphost")).Port(port).Db(db).Password(beego.AppConfig.String("ippassword")) c, err := redis.NewSynchClientWithSpec(spec) //随机取IP if err != nil { println("数据库连接失败") } else { client = c } }
func TestAlphazeroRedisConnect(t *testing.T) { var err error alphazeroRedisClient, err = redis.NewSynchClientWithSpec(redis.DefaultSpec()) if err != nil { t.Fatalf("Connect failed: %v", err) } err = alphazeroRedisClient.Ping() if err != nil { t.Fatalf("Command failed: %v", err) } }
func GetRedisClient() (redis.Client, error) { redis_pass, e := GetConfValue("redis.password") if e != nil { return nil, e } redis_db, e := GetConfValue("redis.dbnumber") if e != nil { return nil, e } redis_ndb, _ := strconv.ParseInt(redis_db, 10, 32) spec := redis.DefaultSpec().Db(int(redis_ndb)).Password(redis_pass) client, e := redis.NewSynchClientWithSpec(spec) if e != nil { return nil, errors.New("Failed to create redis client, wrong password ?") } return client, nil }
func redisConnect() (rd redis.Client, err error) { spec := redis.DefaultSpec().Db(AppConfig.redisDb) if AppConfig.redisHost != "" { spec.Host(AppConfig.redisHost) } if AppConfig.redisAuth != "" { spec.Password(AppConfig.redisAuth) } port := AppConfig.redisPort if port > 0 && port < 65535 { spec.Port(port) } rd, err = redis.NewSynchClientWithSpec(spec) if err != nil { fmt.Printf("Connect Redis: %s", err) return rd, err } return rd, nil }
func NewPipelineRedis(host string, port int, db int, passwd string) *PipelineRedis { spec := redis.DefaultSpec().Host(host).Port(port).Db(db).Password(passwd) client, _ := redis.NewSynchClientWithSpec(spec) return &PipelineRedis{client: client} }
package main import ( redis "github.com/alphazero/Go-Redis" mongo "labix.org/v2/mgo" "log" ) const ( MONGO_CLIENT_URL = "localhost" ) var ( /// redis redisSpec = redis.DefaultSpec() redisConn redis.Client redisError redis.Error /// mongo mongoClient *mongo.Session mongoError error ) func setupRedisConnection() { log.Printf("connecting to redis...") redisConn, redisError = redis.NewSynchClientWithSpec(redisSpec) if redisError != nil { log.Fatal("couldn't connect to redis: ", redisError) } log.Printf("connected to redis.") }