Skip to content

Commit

Permalink
cache#Get requires write lock. #19
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jan 13, 2020
1 parent 2d52e1a commit b3aab73
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var SelectorCacheMaxEntries = 50
var (
cacheOnce sync.Once
cache *lru.Cache
cacheMutex sync.RWMutex
cacheMutex sync.Mutex
)

func getQuery(expr string) (*xpath.Expr, error) {
Expand All @@ -27,14 +27,11 @@ func getQuery(expr string) (*xpath.Expr, error) {
cacheOnce.Do(func() {
cache = lru.New(SelectorCacheMaxEntries)
})
cacheMutex.RLock()
cacheMutex.Lock()
defer cacheMutex.Unlock()
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 b3aab73

Please sign in to comment.