-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache.go
52 lines (45 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
// Copyright 2022 Azugo. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package core
import (
"azugo.io/core/cache"
)
func (a *App) initCache() error {
if a.cache != nil {
return nil
}
conf := a.Config().Cache
opts := []cache.CacheOption{
conf.Type,
cache.Instrumenter(a.Instrumenter()),
}
if conf.TTL > 0 {
opts = append(opts, cache.DefaultTTL(conf.TTL))
}
if len(conf.ConnectionString) != 0 {
opts = append(opts, cache.ConnectionString(conf.ConnectionString))
}
if len(conf.Password) != 0 {
opts = append(opts, cache.ConnectionPassword(conf.Password))
}
if len(conf.KeyPrefix) != 0 {
opts = append(opts, cache.KeyPrefix(conf.KeyPrefix))
}
a.cache = cache.New(opts...)
return a.cache.Start(a.BackgroundContext())
}
func (a *App) closeCache() {
if a.cache == nil {
return
}
a.cache.Close()
}
func (a *App) Cache() *cache.Cache {
if a.cache == nil {
if err := a.initCache(); err != nil {
panic(err)
}
}
return a.cache
}