From 29c5ac780efc692efb427b5f8863607c86133b38 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Mon, 13 Jan 2020 19:35:21 +0800 Subject: [PATCH] add `write` lock for cache#Get --- cache.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/cache.go b/cache.go index 00d3bf4..27b7a32 100644 --- a/cache.go +++ b/cache.go @@ -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) { @@ -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