Compare commits

..

No commits in common. "2341b7e47314a971642920f63d44a36d48f80566" and "9d6d9b9dfffea66d0d5488e2ef79f9664bc95725" have entirely different histories.

2 changed files with 5 additions and 8 deletions

View file

@ -2,10 +2,6 @@
## Unreleased
## v0.1.1 (2025-01-18)
* Fix a deadlock in tests
## v0.1.0 (2025-01-18)
* First version

View file

@ -23,13 +23,14 @@ 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{},
}
}
@ -46,6 +47,9 @@ 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)
@ -54,9 +58,6 @@ 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}
}