Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 committed Apr 18, 2023
1 parent e75ddf4 commit 160c406
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions planner/core/plan_cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type LRUPlanCache struct {
buckets map[string]map[*list.Element]struct{}
lruList *list.List
// lock make cache thread safe
lock syncutil.Mutex
lock syncutil.RWMutex
// onEvict will be called if any eviction happened, only for test use now
onEvict func(kvcache.Key, kvcache.Value)

Expand Down Expand Up @@ -90,8 +90,8 @@ func strHashKey(key kvcache.Key, deepCopy bool) string {

// Get tries to find the corresponding value according to the given key.
func (l *LRUPlanCache) Get(key kvcache.Key, opts *utilpc.PlanCacheMatchOpts) (value kvcache.Value, ok bool) {
l.lock.Lock()
defer l.lock.Unlock()
l.lock.RLock()
defer l.lock.RUnlock()

bucket, bucketExist := l.buckets[strHashKey(key, false)]
if bucketExist {
Expand Down Expand Up @@ -154,6 +154,9 @@ func (l *LRUPlanCache) Delete(key kvcache.Key) {

// DeleteAll deletes all elements from the LRU Cache.
func (l *LRUPlanCache) DeleteAll() {
if l == nil {
return
}
l.lock.Lock()
defer l.lock.Unlock()

Expand All @@ -172,8 +175,8 @@ func (l *LRUPlanCache) DeleteAll() {

// Size gets the current cache size.
func (l *LRUPlanCache) Size() int {
l.lock.Lock()
defer l.lock.Unlock()
l.lock.RLock()
defer l.lock.RUnlock()

return int(l.size)
}
Expand All @@ -198,14 +201,13 @@ func (l *LRUPlanCache) MemoryUsage() (sum int64) {
if l == nil {
return
}
l.lock.RLock()
defer l.lock.RUnlock()
return l.memoryUsageTotal
}

// Close do some clean work for LRUPlanCache when close the session
func (l *LRUPlanCache) Close() {
if l == nil {
return
}
l.DeleteAll()
}

Expand Down

0 comments on commit 160c406

Please sign in to comment.