Compare commits
3 commits
1-finilize
...
main
Author | SHA1 | Date | |
---|---|---|---|
2ee001b76a | |||
2341b7e473 | |||
35044ad38a |
3 changed files with 13 additions and 8 deletions
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
## v0.1.1 (2025-01-18)
|
||||||
|
|
||||||
|
* Fix a deadlock in tests
|
||||||
|
|
||||||
## v0.1.0 (2025-01-18)
|
## v0.1.0 (2025-01-18)
|
||||||
|
|
||||||
* First version
|
* First version
|
||||||
|
|
8
LICENSE
8
LICENSE
|
@ -1,10 +1,12 @@
|
||||||
MIT License Copyright (c) 2021 Bruno Carlin
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2021-2025 Bruno Carlin
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
in the Software without restriction, including without limitation the rights
|
in the Software without restriction, including without limitation the rights
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell c
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
pies of the Software, and to permit persons to whom the Software is
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
furnished to do so, subject to the following conditions:
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
|
9
cache.go
9
cache.go
|
@ -23,14 +23,13 @@ func (e entry[V]) isExpired() bool {
|
||||||
// Cache is an in-memory key-value store.
|
// Cache is an in-memory key-value store.
|
||||||
type Cache[K comparable, V any] struct {
|
type Cache[K comparable, V any] struct {
|
||||||
data map[K]entry[V]
|
data map[K]entry[V]
|
||||||
mu *sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// New instantiate a new cache.
|
// New instantiate a new cache.
|
||||||
func New[K comparable, V any]() *Cache[K, V] {
|
func New[K comparable, V any]() *Cache[K, V] {
|
||||||
return &Cache[K, V]{
|
return &Cache[K, V]{
|
||||||
data: make(map[K]entry[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.
|
// A 0 ttl value disables the expiration of the value.
|
||||||
func (c *Cache[K, V]) PutTTL(key K, val V, ttl time.Duration) {
|
func (c *Cache[K, V]) PutTTL(key K, val V, ttl time.Duration) {
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
if ttl == 0 {
|
if ttl == 0 {
|
||||||
c.Put(key, val)
|
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)
|
exp := time.Now().Add(ttl)
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
c.data[key] = entry[V]{&exp, val}
|
c.data[key] = entry[V]{&exp, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue