Skip to content

Commit

Permalink
#26 add mutex for the query caching
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Dec 23, 2019
1 parent 16f1e6c commit 899f3c8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var SelectorCacheMaxEntries = 50
var (
cacheOnce sync.Once
cache *lru.Cache
cacheMutex sync.RWMutex
)

func getQuery(expr string) (*xpath.Expr, error) {
Expand All @@ -27,9 +28,14 @@ func getQuery(expr string) (*xpath.Expr, error) {
cacheOnce.Do(func() {
cache = lru.New(50)
})
cacheMutex.RLock()
if v, ok := cache.Get(expr); ok {
cacheMutex.RUnlock()
return v.(*xpath.Expr), nil
}
cacheMutex.RUnlock()
cacheMutex.Lock()
defer cacheMutex.Unlock()
v, err := xpath.Compile(expr)
if err != nil {
return nil, err
Expand Down

0 comments on commit 899f3c8

Please sign in to comment.