manager.go 754 字节
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package redis_manager

import (
	"sync"
)

var (
Y
Your Name 已提交
8
	redisOfCluster   = make(map[string]Redis)
E
eoLinker API Management 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	redisConfCluster = make(map[string]RedisConfig)

	locker sync.RWMutex
)

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
}
func Get(name string) (Redis, bool) {
Y
Your Name 已提交
31
	r, has := get(name)
E
eoLinker API Management 已提交
32

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

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

	redisOfCluster[name] = nil

Y
Your Name 已提交
52
	return nil, false
E
eoLinker API Management 已提交
53
}