/
recache.go
53 lines (42 loc) · 988 Bytes
/
recache.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
// Package recache provides a regex compilation cache.
package recache
import (
"regexp"
"time"
"github.com/patrickmn/go-cache"
)
// Expiration intervals.
const (
DefaultExpiration = 5 * time.Minute
DefaultCleanupInterval = 10 * time.Minute
)
// RegexpCache caches compiled regular expressions.
type RegexpCache struct {
c *cache.Cache
}
// New creates a new RegexpCache.
func New() *RegexpCache {
return &RegexpCache{
c: cache.New(DefaultExpiration, DefaultCleanupInterval),
}
}
// Compile compiles a pattern, forcing case-insensitive matching. If this
// pattern is cached, the existing Regexp/error will be returned.
func (r *RegexpCache) Compile(pattern string) (*regexp.Regexp, error) {
type value struct {
re *regexp.Regexp
err error
}
got, ok := r.c.Get(pattern)
if ok {
v := got.(*value)
return v.re, v.err
}
re, err := regexp.Compile(`(?i)` + pattern)
v := &value{
re: re,
err: err,
}
r.c.SetDefault(pattern, v)
return re, err
}