Skip to content

Commit

Permalink
add write lock for cache#Get
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jan 13, 2020
1 parent 7da4976 commit 29c5ac7
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var DisableSelectorCache = false
var SelectorCacheMaxEntries = 50

var (
cacheOnce sync.Once
cache *lru.Cache
cacheMutex sync.RWMutex
cacheOnce sync.Once
cache *lru.Cache
cacheMutex sync.Mutex
)

func getQuery(expr string) (*xpath.Expr, error) {
Expand All @@ -28,14 +28,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 29c5ac7

Please sign in to comment.