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