forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_regexp.go
59 lines (50 loc) · 930 Bytes
/
cache_regexp.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
package regexp
import (
"regexp"
"time"
)
type regexpCache struct {
*cache
noCacheFunc func(string) (*regexp.Regexp, error)
}
func newRegexpCache(ttl time.Duration, isEnabled bool, fn func(string) (*regexp.Regexp, error)) *regexpCache {
return ®expCache{
cache: newCache(
ttl,
isEnabled,
),
noCacheFunc: fn,
}
}
func (c *regexpCache) doNoCacheFunc(str string) (*Regexp, error) {
rx, err := c.noCacheFunc(str)
if err != nil {
return nil, err
}
return &Regexp{
rx,
false,
},
nil
}
func (c *regexpCache) do(str string) (*Regexp, error) {
// return if cache is not enabled
if !c.enabled() {
return c.doNoCacheFunc(str)
}
// cache hit
if rx, found := c.getRegexp(str); found {
return &Regexp{
rx,
true,
},
nil
}
// cache miss, add to cache
regExp, err := c.doNoCacheFunc(str)
if err != nil {
return nil, err
}
c.add(str, regExp.Regexp)
return regExp, nil
}