本文整理匯總了Golang中github.com/garyburd/redigo/redis.DialDatabase函數的典型用法代碼示例。如果您正苦於以下問題:Golang DialDatabase函數的具體用法?Golang DialDatabase怎麽用?Golang DialDatabase使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了DialDatabase函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newPool
func newPool(server, password string, database int, isRedis bool, maxActive int, wait bool) *redis.Pool {
return &redis.Pool{
MaxIdle: 50,
MaxActive: maxActive,
Wait: wait,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server, redis.DialDatabase(database))
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
if isRedis {
if _, err := c.Do("CLIENT", "SETNAME", "bosun"); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
}
}
示例2: Dial
func (m MRedis) Dial(s string) {
c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2))
if err != nil {
panic(err)
}
defer c.Close()
}
示例3: testTopologyInRedis
func testTopologyInRedis(t *testing.T, cfg map[string]interface{}) {
tests := []struct {
out *redisOut
name string
ips []string
}{
{nil, "proxy1", []string{"10.1.0.4"}},
{nil, "proxy2", []string{"10.1.0.9", "fe80::4e8d:79ff:fef2:de6a"}},
{nil, "proxy3", []string{"10.1.0.10"}},
}
db := 0
index := cfg["index"].(string)
if v, ok := cfg["db_topology"]; ok {
db = v.(int)
}
// prepare redis
{
conn, err := redis.Dial("tcp", getRedisAddr(), redis.DialDatabase(db))
if err != nil {
t.Fatalf("redis.Dial failed %v", err)
}
// delete old key if present
defer conn.Close()
conn.Do("DEL", index)
}
// 1. connect
for i := range tests {
tests[i].out = newRedisTestingOutput(t, cfg)
defer tests[i].out.Close()
}
// 2. publish ips twice (so all outputs have same topology map)
for i := 0; i < 2; i++ {
for _, test := range tests {
t.Logf("publish %v ips: %v", test.name, test.ips)
err := test.out.PublishIPs(test.name, test.ips)
assert.NoError(t, err)
}
}
// 3. check names available
for _, test := range tests {
t.Logf("check %v knows ips", test.name)
for _, other := range tests {
t.Logf(" check ips of %v", other.name)
for _, ip := range other.ips {
name := test.out.GetNameByIP(ip)
t.Logf(" check ip: %v -> %v", ip, other.name == name)
assert.Equal(t, other.name, name)
}
}
}
}
示例4: Set
func (m MRedis) Set() {
c, err := redis.Dial("tcp", "192.168.176.3:6379", redis.DialDatabase(3))
if err != nil {
panic(err)
}
defer c.Close()
ok, err := redis.String(c.Do("SET", "testkey", "test"))
if err != nil {
panic(err)
}
fmt.Print(ok)
}
示例5: Get
func (m MRedis) Get(s string) {
c, err := redis.Dial("tcp", "192.168.176.3:6379", redis.DialDatabase(2))
if err != nil {
panic(err)
}
defer c.Close()
ok, err := redis.String(c.Do("GET", "service_role_code"))
if err != nil {
panic(err)
}
fmt.Print(ok)
}
示例6: c_redis_counters
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) {
var md opentsdb.MultiDataPoint
conn, err := redis.Dial("tcp", server, redis.DialDatabase(db))
if err != nil {
return md, err
}
defer conn.Close()
if _, err := conn.Do("CLIENT", "SETNAME", "scollector"); err != nil {
return md, err
}
cursor := 0
for {
vals, err := redis.Values(conn.Do("HSCAN", collect.RedisCountersKey, cursor))
if err != nil {
return md, err
}
if len(vals) != 2 {
return md, fmt.Errorf("Unexpected number of values")
}
cursor, err = redis.Int(vals[0], nil)
if err != nil {
return md, err
}
pairs, err := redis.StringMap(vals[1], nil)
if err != nil {
return md, err
}
for mts, val := range pairs {
parts := strings.Split(mts, ":")
if len(parts) != 2 {
slog.Errorf("Invalid metric tag set counter: %s", mts)
continue
}
metric := parts[0]
tags, err := opentsdb.ParseTags(parts[1])
if err != nil {
slog.Errorf("Invalid tags: %s", parts[1])
continue
}
v, err := strconv.Atoi(val)
if err != nil {
slog.Errorf("Invalid counter value: %s", val)
continue
}
Add(&md, metric, v, tags, metadata.Counter, metadata.Count, "")
}
if cursor == 0 {
break
}
}
return md, nil
}
示例7: c_redis_counters
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) {
var md opentsdb.MultiDataPoint
conn, err := redis.Dial("tcp", server, redis.DialDatabase(db))
if err != nil {
return md, slog.Wrap(err)
}
defer conn.Close()
//do a dance to detect proper hscan command for ledis or redis
hscanCmd := "XHSCAN"
info, err := redis.String(conn.Do("info", "server"))
if err != nil {
return md, slog.Wrap(err)
}
if strings.Contains(info, "redis_version") {
hscanCmd = "HSCAN"
}
cursor := "0"
for {
vals, err := redis.Values(conn.Do(hscanCmd, collect.RedisCountersKey, cursor))
if err != nil {
return md, slog.Wrap(err)
}
if len(vals) != 2 {
return md, fmt.Errorf("Unexpected number of values")
}
cursor, err = redis.String(vals[0], nil)
if err != nil {
return md, slog.Wrap(err)
}
pairs, err := redis.StringMap(vals[1], nil)
if err != nil {
return md, slog.Wrap(err)
}
for key, val := range pairs {
ak := models.AlertKey(key)
v, err := strconv.Atoi(val)
if err != nil {
slog.Errorf("Invalid counter value: %s", val)
continue
}
Add(&md, ak.Name(), v, ak.Group(), metadata.Counter, metadata.Count, "")
}
if cursor == "" || cursor == "0" {
break
}
}
return md, nil
}
示例8: Hmget
func (m MRedis) Hmget(s string) {
c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2))
if err != nil {
panic(err)
}
defer c.Close()
ok, err := redis.Strings(c.Do("HMGET", "w.pplive.com", "cache_key_without_args", "sorted_src_ip_usability_list"))
if err != nil {
panic(err)
}
for k, v := range ok {
fmt.Printf("%d = %s\n", k, v)
}
}
示例9: Connect
//連接
func (p *Client_t) Connect(ip string, port uint16, dataBases int) (err error) {
p.ip = ip
p.port = port
p.dataBases = dataBases
var addr = ip + ":" + strconv.Itoa(int(port))
dialOption := redis.DialDatabase(dataBases)
p.Conn, err = redis.Dial("tcp", addr, dialOption)
if nil != err {
fmt.Println("######redis.Dial err:", err, ip, port, dataBases)
return err
}
return err
}
示例10: newRedisPool
func newRedisPool(server string, database int) *redis.Pool {
return &redis.Pool{
MaxIdle: 10,
MaxActive: 10,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server, redis.DialDatabase(database))
if err != nil {
return nil, err
}
return c, err
},
}
}
示例11: Hgetall
func (m MRedis) Hgetall(s string) {
c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2))
if err != nil {
panic(err)
}
defer c.Close()
ok, err := redis.StringMap(c.Do("HGETALL", "*.pplive.com"))
if err != nil {
panic(err)
}
for k, v := range ok {
fmt.Printf("%s = %s\n", k, v)
}
}
示例12: InitRedis
func InitRedis() {
redisDB := redigo.DialDatabase(config.DatabaseID)
redisPool = &redigo.Pool{
MaxIdle: 10,
IdleTimeout: 1 * time.Second,
Dial: func() (redigo.Conn, error) {
return redigo.Dial("tcp", config.RedisServerAndPort, redisDB)
},
TestOnBorrow: func(c redigo.Conn, t time.Time) (err error) {
_, err = c.Do("PING")
if err != nil {
panic("Error connecting to redis")
}
return
},
}
}
示例13: newRedisPool
func newRedisPool(server string, database int) *redis.Pool {
return &redis.Pool{
MaxIdle: 10,
MaxActive: 10,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server, redis.DialDatabase(database))
if err != nil {
return nil, err
}
if _, err := c.Do("CLIENT", "SETNAME", metricRoot+"_counters"); err != nil {
c.Close()
return nil, err
}
return c, err
},
}
}
示例14: startRedis
func startRedis() redis.Conn {
addr := os.Getenv("REDIS_ADDR")
if addr == "" {
addr = redisDefaultAddress
}
dbIndex, err := strconv.Atoi(os.Getenv("REDIS_DB"))
if err != nil {
dbIndex = redisDefaultDDb
}
client, err := redis.Dial("tcp", addr, redis.DialDatabase(dbIndex))
if err != nil {
panic("Failed to start redis.\n" + err.Error())
}
log.Printf("Redis started addr=%s dbIndex=%d.\n", addr, dbIndex)
return client
}
示例15: NewPool
// NewPool creates a redis pool connected to the given host:port and db.
func NewPool(host string, port, db uint) (*Wredis, error) {
if host == "" {
return nil, errors.New("host cannot be empty")
}
if port == 0 {
return nil, errors.New("port cannot be 0")
}
addr := fmt.Sprintf("%s:%d", host, int(port))
pool := &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", addr, redis.DialDatabase(int(db)))
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
return &Wredis{pool, true}, nil
}