forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
88 lines (68 loc) · 1.55 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package regexp
import (
"regexp"
"time"
gocache "github.com/pmylund/go-cache"
)
const (
defaultCacheItemTTL = 60 * time.Second
defaultCacheCleanupInterval = 5 * time.Minute
maxKeySize = 1024
maxValueSize = 2048
)
type cache struct {
*gocache.Cache
isEnabled bool
ttl time.Duration
}
func newCache(ttl time.Duration, isEnabled bool) *cache {
return &cache{
Cache: gocache.New(ttl, defaultCacheCleanupInterval),
isEnabled: isEnabled,
ttl: ttl,
}
}
func (c *cache) enabled() bool {
return c.isEnabled && c.Cache != nil
}
func (c *cache) add(key string, value interface{}) {
c.Add(key, value, c.ttl)
}
func (c *cache) getRegexp(key string) (*regexp.Regexp, bool) {
if val, found := c.Get(key); found {
return val.(*regexp.Regexp).Copy(), true
}
return nil, false
}
func (c *cache) getString(key string) (string, bool) {
if val, found := c.Get(key); found {
return val.(string), true
}
return "", false
}
func (c *cache) getStrSlice(key string) ([]string, bool) {
if val, found := c.Get(key); found {
return val.([]string), true
}
return []string{}, false
}
func (c *cache) getStrSliceOfSlices(key string) ([][]string, bool) {
if val, found := c.Get(key); found {
return val.([][]string), true
}
return [][]string{}, false
}
func (c *cache) getBool(key string) (bool, bool) {
if val, found := c.Get(key); found {
return val.(bool), true
}
return false, false
}
func (c *cache) reset(ttl time.Duration, isEnabled bool) {
if c.Cache == nil {
return
}
c.isEnabled = isEnabled
c.ttl = ttl
c.Flush()
}