-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice_provider.go
More file actions
58 lines (45 loc) · 1.3 KB
/
service_provider.go
File metadata and controls
58 lines (45 loc) · 1.3 KB
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
55
56
57
58
package redis
import (
"github.com/goal-web/contracts"
"sync"
)
var (
// factory 默认 redis 客户端
factory contracts.RedisFactory
cli contracts.RedisConnection
)
// Default 用于不方便注入的地方使用,请确保已经注册了 redis 服务
func Default() contracts.RedisConnection {
if cli == nil {
cli = factory.Connection()
}
return cli
}
// DefaultFactory 用于不方便注入的地方使用,请确保已经注册了 redis 服务
func DefaultFactory() contracts.RedisFactory {
return factory
}
type ServiceProvider struct {
}
func (this ServiceProvider) Stop() {
}
func (this ServiceProvider) Start() error {
return nil
}
func (this ServiceProvider) Register(app contracts.Application) {
app.Singleton("redis.factory", func(config contracts.Config, handler contracts.ExceptionHandler) contracts.RedisFactory {
factory = &Factory{
config: config.Get("redis").(Config),
exceptionHandler: handler,
connections: make(map[string]contracts.RedisConnection),
mutex: sync.Mutex{},
}
return factory
})
app.Singleton("redis", func(factory contracts.RedisFactory) contracts.RedisConnection {
return factory.Connection()
})
app.Singleton("redis.connection", func(redis contracts.RedisConnection) *Connection {
return redis.(*Connection)
})
}