openvpn-mgt/vendor/github.com/pyke369/golang-support/rcache/rcache.go

43 lines
658 B
Go
Raw Normal View History

2019-07-08 20:32:12 +00:00
package rcache
import (
"crypto/md5"
"regexp"
"runtime"
"sync"
)
var (
cores int
cache map[[16]byte]*regexp.Regexp = map[[16]byte]*regexp.Regexp{}
lock sync.RWMutex
)
func Get(expression string) *regexp.Regexp {
if cores == 0 {
cores = runtime.NumCPU()
}
key := md5.Sum([]byte(expression))
if cores > 1 {
lock.RLock()
}
if cache[key] != nil {
if cores > 1 {
defer lock.RUnlock()
}
return cache[key].Copy()
}
if cores > 1 {
lock.RUnlock()
}
if regex, err := regexp.Compile(expression); err == nil {
if cores > 1 {
lock.Lock()
defer lock.Unlock()
}
cache[key] = regex
return cache[key].Copy()
}
return nil
}