bbgo_origin/pkg/util/volatile_memory.go

44 lines
847 B
Go
Raw Permalink Normal View History

2020-07-10 13:34:39 +00:00
package util
import "time"
type VolatileMemory struct {
objectTimes map[interface{}]time.Time
textTimes map[string]time.Time
}
func NewDetectorCache() *VolatileMemory {
return &VolatileMemory{
objectTimes: make(map[interface{}]time.Time),
textTimes: make(map[string]time.Time),
}
}
func (i *VolatileMemory) IsObjectFresh(obj interface{}, ttl time.Duration) bool {
now := time.Now()
outdatedTime := now.Add(-ttl)
if hitTime, ok := i.objectTimes[obj]; ok {
if hitTime.Before(outdatedTime) {
return true
}
}
i.objectTimes[obj] = now
return false
}
func (i *VolatileMemory) IsTextFresh(text string, ttl time.Duration) bool {
now := time.Now()
outdatedTime := now.Add(-ttl)
if hitTime, ok := i.textTimes[text]; ok {
if hitTime.Before(outdatedTime) {
return true
}
}
i.textTimes[text] = now
return false
}