1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-25 23:03:47 +01:00

chore: fixes race condition (#2959)

This commit is contained in:
Amir Raminfar
2024-05-13 15:32:51 -07:00
committed by GitHub
parent b1f980f49b
commit cbda1e753c

View File

@@ -1,6 +1,7 @@
package cache
import (
"sync"
"time"
log "github.com/sirupsen/logrus"
@@ -11,6 +12,7 @@ type Cache[T any] struct {
Timestamp time.Time
Duration time.Duration
Data T
mu sync.Mutex
}
func New[T any](f func() (T, error), duration time.Duration) *Cache[T] {
@@ -21,6 +23,8 @@ func New[T any](f func() (T, error), duration time.Duration) *Cache[T] {
}
func (c *Cache[T]) GetWithHit() (T, error, bool) {
c.mu.Lock()
defer c.mu.Unlock()
hit := true
if c.Timestamp.IsZero() || time.Since(c.Timestamp) > c.Duration {
hit = false