-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
54 lines (44 loc) · 1.3 KB
/
factory.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
51
52
53
54
package cache
import (
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
)
type Factory struct {
config Config
exceptionHandler contracts.ExceptionHandler
stores map[string]contracts.CacheStore
drivers map[string]contracts.CacheStoreProvider
}
func (factory *Factory) getName(names ...string) string {
if len(names) > 0 {
return names[0]
}
return factory.config.Default
}
func (factory Factory) getConfig(name string) contracts.Fields {
return factory.config.Stores[name]
}
func (factory *Factory) Store(names ...string) contracts.CacheStore {
name := factory.getName(names...)
if cacheStore, existsStore := factory.stores[name]; existsStore {
return cacheStore
}
factory.stores[name] = factory.make(name)
return factory.stores[name]
}
func (factory *Factory) Extend(driver string, cacheStoreProvider contracts.CacheStoreProvider) {
factory.drivers[driver] = cacheStoreProvider
}
func (factory *Factory) make(name string) contracts.CacheStore {
config := factory.getConfig(name)
driver := utils.GetStringField(config, "driver")
driveProvider, existsProvider := factory.drivers[driver]
if !existsProvider {
panic(DriverException{
fmt.Errorf("不支持的缓存驱动:%s", driver),
config,
})
}
return driveProvider(config)
}