2023-07-07 06:07:10 +00:00
|
|
|
package timecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FirstSeenCache is a time cache that only marks the expiry of a message when first added.
|
|
|
|
type FirstSeenCache struct {
|
|
|
|
lk sync.RWMutex
|
|
|
|
m map[string]time.Time
|
|
|
|
ttl time.Duration
|
|
|
|
|
|
|
|
done func()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ TimeCache = (*FirstSeenCache)(nil)
|
|
|
|
|
|
|
|
func newFirstSeenCache(ttl time.Duration) *FirstSeenCache {
|
|
|
|
tc := &FirstSeenCache{
|
|
|
|
m: make(map[string]time.Time),
|
|
|
|
ttl: ttl,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, done := context.WithCancel(context.Background())
|
|
|
|
tc.done = done
|
|
|
|
go background(ctx, &tc.lk, tc.m)
|
|
|
|
|
|
|
|
return tc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *FirstSeenCache) Done() {
|
|
|
|
tc.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *FirstSeenCache) Has(s string) bool {
|
|
|
|
tc.lk.RLock()
|
|
|
|
|
|
|
|
_, ok := tc.m[s]
|
2024-10-12 18:48:25 +00:00
|
|
|
tc.lk.RUnlock()
|
2023-07-07 06:07:10 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *FirstSeenCache) Add(s string) bool {
|
|
|
|
tc.lk.Lock()
|
|
|
|
|
|
|
|
_, ok := tc.m[s]
|
|
|
|
if ok {
|
2024-10-12 18:48:25 +00:00
|
|
|
tc.lk.Unlock()
|
2023-07-07 06:07:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.m[s] = time.Now().Add(tc.ttl)
|
2024-10-12 18:48:25 +00:00
|
|
|
tc.lk.Unlock()
|
2023-07-07 06:07:10 +00:00
|
|
|
return true
|
|
|
|
}
|