Blame view

utils/db/redis.go 950 Bytes
cad2b7f3   zhangqijia   reactor: 重构目录, 重构...
1
2
3
4
  package db
  
  import (
  	"github.com/garyburd/redigo/redis"
cad2b7f3   zhangqijia   reactor: 重构目录, 重构...
5
6
7
8
  	"time"
  )
  var RedisPool *redis.Pool
  
5d9cf01c   zhangqijia   plugin 热更
9
10
  //conf *conf.ServerConf
  func ConnectRedis(db int, auth, address string) error {
cad2b7f3   zhangqijia   reactor: 重构目录, 重构...
11
12
13
14
15
16
17
18
  	RedisPool = &redis.Pool{
  		//最大活跃连接数,0代表无限
  		MaxActive: 888,
  		MaxIdle: 20,
  		//闲置连接的超时时间
  		IdleTimeout: time.Second * 100,
  		//定义拨号获得连接的函数
  		Dial: func() (redis.Conn, error) {
5d9cf01c   zhangqijia   plugin 热更
19
20
21
  			option := []redis.DialOption{redis.DialDatabase(db)}
  			if auth != "" {
  				option = append(option, redis.DialPassword(auth))
cad2b7f3   zhangqijia   reactor: 重构目录, 重构...
22
  			}
5d9cf01c   zhangqijia   plugin 热更
23
  			return redis.Dial("tcp",address, option...)
cad2b7f3   zhangqijia   reactor: 重构目录, 重构...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  		},
  	}
  	return nil
  }
  
  func CloseRedis()  {
  	RedisPool.Close()
  }
  
  func HKEYS(args ...interface{}) (reply interface{}, err error) {
  	conn := RedisPool.Get()
  	defer conn.Close()
  	return conn.Do("HKEYS", args)
  }
  
  func HMSET(args ...interface{})  (reply interface{}, err error) {
  	conn := RedisPool.Get()
  	defer conn.Close()
  	return conn.Do("HMSET", args)
  }