LRU package is already threadsafe

This commit is contained in:
Neil Alexander 2020-12-16 11:08:28 +00:00
parent 14c8fa3deb
commit cfee085119
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -2,7 +2,6 @@ package caching
import (
"fmt"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/client_golang/prometheus"
@ -88,7 +87,6 @@ type InMemoryLRUCachePartition struct {
name string
mutable bool
maxEntries int
mutex sync.RWMutex
lru *lru.Cache
}
@ -116,8 +114,6 @@ func NewInMemoryLRUCachePartition(name string, mutable bool, maxEntries int, ena
}
func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.mutable {
if peek, ok := c.lru.Peek(key); ok && peek != value {
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key))
@ -127,8 +123,6 @@ func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
}
func (c *InMemoryLRUCachePartition) Unset(key string) {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.mutable {
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %q", key))
}
@ -136,7 +130,5 @@ func (c *InMemoryLRUCachePartition) Unset(key string) {
}
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.lru.Get(key)
}