Blame view

components/db/redis.go 1.19 KB
ee23102d   zhangqijia   支持mongo, grpc接服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  package db
  
  import (
  	"fmt"
  	"github.com/garyburd/redigo/redis"
  	"pro2d/conf"
  	"pro2d/utils"
  	"time"
  )
  
  type RedisPool struct {
  	RedisPool *redis.Pool
  }
  
  func (rp *RedisPool)Connect(conf *conf.ServerConf) error {
  	rp.RedisPool = &redis.Pool{
  		//最大活跃连接数,0代表无限
  		MaxActive: 888,
  		MaxIdle: 20,
  		//闲置连接的超时时间
  		IdleTimeout: time.Second * 100,
  		//定义拨号获得连接的函数
  		Dial: func() (redis.Conn, error) {
  			option := []redis.DialOption{redis.DialDatabase(conf.RedisConf.DB)}
  			if conf.RedisConf.Auth != "" {
  				option = append(option, redis.DialPassword(conf.RedisConf.Auth))
  			}
  			return redis.Dial("tcp",conf.RedisConf.Address, option...)
  		},
  	}
  	return nil
  }
  
  func (rp *RedisPool)Close()  {
  	rp.RedisPool.Close()
  }
  
  func (rp *RedisPool) Insert() error {
  	conn := rp.RedisPool.Get()
  	defer conn.Close()
  	reply, err := conn.Do("HKEYS", fmt.Sprintf("account:%s", "123123"))
  	if err != nil {
  		return err
  	}
  
  	utils.Sugar.Debugf("%v", reply)
  	reply, err = conn.Do("HMSET", fmt.Sprintf("account:%s", "1231231"), "phone", "1231231", "passwd", "2131231")
  	if err != nil {
  		utils.Sugar.Errorf("%v", err)
  		return err
  	}
  	utils.Sugar.Debugf("%v", reply)
  	return nil
  }