-
Notifications
You must be signed in to change notification settings - Fork 164
/
interactWithCache.go
62 lines (44 loc) · 1.2 KB
/
interactWithCache.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
59
60
61
62
package kernel
import (
"github.com/ArtisanCloud/PowerLibs/v2/cache"
"time"
)
type InteractsWithCache struct {
Cache CacheInterface
}
type CacheInterface cache.CacheInterface
type RedisOptions cache.RedisOptions
func NewRedisClient(options *RedisOptions) CacheInterface {
if options == nil {
return nil
}
if options.Addr == "" {
return nil
}
return cache.NewGRedis((*cache.RedisOptions)(options))
}
func NewInteractsWithCache(client CacheInterface) *InteractsWithCache {
interact := &InteractsWithCache{
Cache: client,
}
if client == nil {
interact.Cache = interact.createDefaultCache()
}
return interact
}
func (interactCache *InteractsWithCache) GetCache() CacheInterface {
if interactCache.Cache != nil {
return interactCache.Cache
}
// create default cache
interactCache.Cache = interactCache.createDefaultCache()
return interactCache.Cache
}
func (interactCache *InteractsWithCache) SetCache(cache CacheInterface) *InteractsWithCache {
interactCache.Cache = cache
return interactCache
}
func (interactCache *InteractsWithCache) createDefaultCache() CacheInterface {
interactCache.Cache = cache.NewMemCache("ac.go.power", time.Duration(1500), "")
return interactCache.Cache
}