manager.go 814 字节
Newer Older
Y
Your Name 已提交
1
package redismanager
E
eoLinker API Management 已提交
2 3 4 5 6 7

import (
	"sync"
)

var (
Y
Your Name 已提交
8
	redisOfCluster   = make(map[string]Redis)
E
eoLinker API Management 已提交
9 10 11 12 13
	redisConfCluster = make(map[string]RedisConfig)

	locker sync.RWMutex
)

Y
Your Name 已提交
14
//InitRedisOfCluster 初始化集群redis
E
eoLinker API Management 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
func InitRedisOfCluster(rs map[string]RedisConfig) {
	locker.Lock()
	defer locker.Unlock()

	redisConfCluster = rs

}

func get(name string) (Redis, bool) {
	locker.RLock()
	defer locker.RUnlock()

	r, h := redisOfCluster[name]

	return r, h
}
Y
Your Name 已提交
31 32

//Get 获取redis
E
eoLinker API Management 已提交
33
func Get(name string) (Redis, bool) {
Y
Your Name 已提交
34
	r, has := get(name)
E
eoLinker API Management 已提交
35

Y
Your Name 已提交
36 37
	if has {
		return r, r != nil
E
eoLinker API Management 已提交
38 39 40 41 42
	}

	locker.Lock()
	defer locker.Unlock()
	r, has = redisOfCluster[name]
Y
Your Name 已提交
43 44
	if has {
		return r, has
E
eoLinker API Management 已提交
45
	}
Y
Your Name 已提交
46 47
	c, h := redisConfCluster[name]
	if h {
E
eoLinker API Management 已提交
48 49 50 51 52 53 54
		r = Create(c)
		redisOfCluster[name] = r
		return r, h
	}

	redisOfCluster[name] = nil

Y
Your Name 已提交
55
	return nil, false
E
eoLinker API Management 已提交
56
}