fix: deadlock in tests
This commit is contained in:
parent
9d6d9b9dff
commit
35044ad38a
1 changed files with 4 additions and 5 deletions
9
cache.go
9
cache.go
|
@ -23,14 +23,13 @@ func (e entry[V]) isExpired() bool {
|
|||
// Cache is an in-memory key-value store.
|
||||
type Cache[K comparable, V any] struct {
|
||||
data map[K]entry[V]
|
||||
mu *sync.RWMutex
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// New instantiate a new cache.
|
||||
func New[K comparable, V any]() *Cache[K, V] {
|
||||
return &Cache[K, V]{
|
||||
data: make(map[K]entry[V]),
|
||||
mu: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,9 +46,6 @@ func (c *Cache[K, V]) Put(key K, val V) {
|
|||
//
|
||||
// A 0 ttl value disables the expiration of the value.
|
||||
func (c *Cache[K, V]) PutTTL(key K, val V, ttl time.Duration) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if ttl == 0 {
|
||||
c.Put(key, val)
|
||||
|
||||
|
@ -58,6 +54,9 @@ func (c *Cache[K, V]) PutTTL(key K, val V, ttl time.Duration) {
|
|||
|
||||
exp := time.Now().Add(ttl)
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.data[key] = entry[V]{&exp, val}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue