go-libp2p-pubsub/timecache/time_cache.go
Mohsin Zaidi 973fef56e1
feat: expire messages from the cache based on last seen time (#513)
* feat: expire messages from the cache based on last seen time

* chore: minor renaming

* fix: messages should not be found after expiration

* chore: editorial

* fix: use new time cache strategy consistently

* fix: default to old time cache and add todo for background gc
2023-01-24 02:02:44 +02:00

33 lines
690 B
Go

package timecache
import "time"
type Strategy uint8
const (
Strategy_FirstSeen Strategy = iota
Strategy_LastSeen
)
type TimeCache interface {
Add(string)
Has(string) bool
}
// NewTimeCache defaults to the original ("first seen") cache implementation
func NewTimeCache(span time.Duration) TimeCache {
return NewTimeCacheWithStrategy(Strategy_FirstSeen, span)
}
func NewTimeCacheWithStrategy(strategy Strategy, span time.Duration) TimeCache {
switch strategy {
case Strategy_FirstSeen:
return newFirstSeenCache(span)
case Strategy_LastSeen:
return newLastSeenCache(span)
default:
// Default to the original time cache implementation
return newFirstSeenCache(span)
}
}