-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
45 lines (38 loc) · 866 Bytes
/
cache.go
File metadata and controls
45 lines (38 loc) · 866 Bytes
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
package smart
import (
"github.com/astaxie/beego/cache"
cache2 "github.com/goburrow/cache"
"sync"
"tianwei.pro/beego-guava"
"time"
)
type CacheManager interface {
Get(name string) cache.Cache
}
type SmartCacheManager struct {
sync.Mutex
container map[string]cache.Cache
}
func NewSmartCacheManager() CacheManager{
return &SmartCacheManager{
container: make(map[string]cache.Cache),
}
}
func (s *SmartCacheManager) Get(name string) cache.Cache {
if v, ok := s.container[name]; ok {
return v
} else {
s.Lock()
defer s.Unlock()
if v, ok := s.container[name]; ok {
return v
}
c := cache2.NewLoadingCache(func(key cache2.Key) (value cache2.Value, e error) {
return nil, nil
}, cache2.WithMaximumSize(1000),
cache2.WithExpireAfterAccess(30 * time.Hour),)
cc := beego_guava.NewGuava(c)
s.container[name] = cc
return cc
}
}