-
Notifications
You must be signed in to change notification settings - Fork 4
/
cachefactory.go
52 lines (40 loc) · 909 Bytes
/
cachefactory.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
package caches
import (
"reflect"
"sync"
"github.com/mangohow/cloud-ide/cmd/webserver/internal/dao"
)
type cacheFactory struct {
caches map[reflect.Type]interface{}
lock sync.Mutex
}
var (
factory = &cacheFactory{caches: make(map[reflect.Type]interface{})}
)
func CacheFactory() *cacheFactory {
return factory
}
func (f *cacheFactory) TmplCache(dao *dao.SpaceTemplateDao) *TmplCache {
t := reflect.TypeOf(&TmplCache{})
f.lock.Lock()
defer f.lock.Unlock()
if c, ok := f.caches[t]; ok {
return c.(*TmplCache)
}
cache := newTmplCache(dao)
f.caches[t] = cache
cache.LoadCache()
return cache
}
func (f *cacheFactory) SpecCache(dao *dao.SpaceTemplateDao) *SpecCache {
t := reflect.TypeOf(&SpecCache{})
f.lock.Lock()
defer f.lock.Unlock()
if c, ok := f.caches[t]; ok {
return c.(*SpecCache)
}
cache := newSpecCache(dao)
f.caches[t] = cache
cache.LoadCache()
return cache
}