| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
type loadFunc func(key interface{}) (interface{}, error) |
| 9 |
|
| 10 |
const ( |
| 11 |
defaultCap = 65536 |
| 12 |
) |
| 13 |
|
| 14 |
// The reason we're building a simple capacity-resetting loading cache (when capacity reached) instead of using |
| 15 |
// something like github.com/hashicorp/golang-lru is primarily due to (not wanting to create) external dependency. |
| 16 |
// Currently this library has 0 external dep (other than go sdk), and supports go 1.6, 1.9, and 1.10 (and later). |
| 17 |
// Creating external lib dependencies (plus their transitive dependencies) would make things hard if not impossible. |
| 18 |
// We expect under most circumstances, the defaultCap is big enough for any long running services that use this |
| 19 |
// library if their xpath regexp cardinality is low. However, in extreme cases when the capacity is reached, we |
| 20 |
// simply reset the cache, taking a small subsequent perf hit (next to nothing considering amortization) in trade |
| 21 |
// of more complex and less performant LRU type of construct. |
| 22 |
type loadingCache struct { |
| 23 |
sync.RWMutex |
| 24 |
cap int |
| 25 |
load loadFunc |
| 26 |
m map[interface{}]interface{} |
| 27 |
reset int |
| 28 |
} |
| 29 |
|
| 30 |
// NewLoadingCache creates a new instance of a loading cache with capacity. Capacity must be >= 0, or |
| 31 |
// it will panic. Capacity == 0 means the cache growth is unbounded. |
| 32 |
func NewLoadingCache(load loadFunc, capacity int) *loadingCache { |
| 33 |
if capacity < 0 { |
| 34 |
panic("capacity must be >= 0") |
| 35 |
} |
| 36 |
return &loadingCache{cap: capacity, load: load, m: make(map[interface{}]interface{})} |
| 37 |
} |
| 38 |
|
| 39 |
func (c *loadingCache) get(key interface{}) (interface{}, error) { |
| 40 |
c.RLock() |
| 41 |
v, found := c.m[key] |
| 42 |
c.RUnlock() |
| 43 |
if found { |
| 44 |
return v, nil |
| 45 |
} |
| 46 |
v, err := c.load(key) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
c.Lock() |
| 51 |
if c.cap > 0 && len(c.m) >= c.cap { |
| 52 |
c.m = map[interface{}]interface{}{key: v} |
| 53 |
c.reset++ |
| 54 |
} else { |
| 55 |
c.m[key] = v |
| 56 |
} |
| 57 |
c.Unlock() |
| 58 |
return v, nil |
| 59 |
} |
| 60 |
|
| 61 |
var ( |
| 62 |
// RegexpCache is a loading cache for string -> *regexp.Regexp mapping. It is exported so that in rare cases |
| 63 |
// client can customize load func and/or capacity. |
| 64 |
RegexpCache = defaultRegexpCache() |
| 65 |
) |
| 66 |
|
| 67 |
func defaultRegexpCache() *loadingCache { |
| 68 |
return NewLoadingCache( |
| 69 |
func(key interface{}) (interface{}, error) { |
| 70 |
return regexp.Compile(key.(string)) |
| 71 |
}, defaultCap) |
| 72 |
} |
| 73 |
|
| 74 |
func getRegexp(pattern string) (*regexp.Regexp, error) { |
| 75 |
exp, err := RegexpCache.get(pattern) |
| 76 |
if err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
return exp.(*regexp.Regexp), nil |
| 80 |
} |