/
cache.go
58 lines (50 loc) · 1.02 KB
/
cache.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
55
56
57
58
package setting
import (
"time"
"github.com/czhj/ahfs/modules/log"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type Cache struct {
Enabled bool
Adapter string
Url string
Interval time.Duration
TTL time.Duration
}
var (
CacheService = struct {
Cache
}{
Cache: Cache{
Enabled: true,
Adapter: "memory",
Interval: 60 * time.Second,
TTL: 24 * time.Hour,
},
}
)
func newCacheService() {
viper.SetDefault("cache", map[string]interface{}{
"enabled": true,
"adapter": "memory",
"interval": 60 * time.Second,
"ttl": 24 * time.Hour,
})
cacheCfg := viper.Sub("cache")
if err := cacheCfg.Unmarshal(&CacheService.Cache); err != nil {
log.Fatal("Cannot unmarshal cache config", zap.Error(err))
}
adapter := CacheService.Adapter
switch adapter {
case "memory":
case "redis", "memcache":
case "":
CacheService.Enabled = false
default:
log.Fatal("Unknow cache adapter", zap.String("adapter", adapter))
}
if CacheService.Enabled {
log.Info("Cache Service Enabled")
}
}