-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_provider.go
50 lines (41 loc) · 1.21 KB
/
service_provider.go
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
package cache
import (
"github.com/goal-web/cache/drivers"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
)
type serviceProvider struct {
}
func NewService() contracts.ServiceProvider {
return serviceProvider{}
}
func (provider serviceProvider) Stop() {
}
func (provider serviceProvider) Start() error {
return nil
}
func (provider serviceProvider) Register(container contracts.Application) {
container.Singleton("cache", func(
config contracts.Config,
redis contracts.RedisFactory,
handler contracts.ExceptionHandler) contracts.CacheFactory {
factory := &Factory{
config: config.Get("cache").(Config),
exceptionHandler: handler,
stores: make(map[string]contracts.CacheStore),
drivers: map[string]contracts.CacheStoreProvider{
"memory": drivers.NewMemory,
},
}
factory.Extend("redis", func(cacheConfig contracts.Fields) contracts.CacheStore {
return drivers.NewRedisCache(
redis.Connection(utils.GetStringField(cacheConfig, "connection")),
utils.GetStringField(cacheConfig, "prefix"),
)
})
return factory
})
container.Singleton("cache.store", func(factory contracts.CacheFactory) contracts.CacheStore {
return factory.Store()
})
}