-
Notifications
You must be signed in to change notification settings - Fork 171
/
interactWithCache.go
64 lines (45 loc) · 1.25 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
63
64
package kernel
import (
"time"
"github.com/ArtisanCloud/PowerLibs/v3/cache"
"github.com/redis/go-redis/v9"
)
type InteractsWithCache struct {
Cache CacheInterface
}
type CacheInterface cache.CacheInterface
type UniversalOptions redis.UniversalOptions
func NewRedisClient(options *UniversalOptions) CacheInterface {
if options == nil {
return nil
}
if len(options.Addrs) == 0 {
return nil
}
return cache.NewGRedis((*redis.UniversalOptions)(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
}