blob: a2366a4329d4008e85648781dcbb7edccdaddd54 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package storage
import (
"sync"
"time"
)
type ReplayCache struct {
ttl time.Duration
mu sync.Mutex
data map[string]time.Time
}
func NewReplayCache(ttl time.Duration) *ReplayCache {
return &ReplayCache{ttl: ttl, data: map[string]time.Time{}}
}
func (c *ReplayCache) CheckAndMark(hash string) (bool, error) {
c.mu.Lock()
defer c.mu.Unlock()
now := time.Now()
for k, ts := range c.data {
if now.Sub(ts) > c.ttl {
delete(c.data, k)
}
}
if _, exists := c.data[hash]; exists {
return true, nil
}
c.data[hash] = now
return false, nil
}
|