modelRT/real-time-data/real_time_cache.go

36 lines
888 B
Go

// Package realtimedata define real time data operation functions
package realtimedata
import (
"context"
"time"
)
// CacheItem define structure for caching real time data with calculation capabilities
type CacheItem struct {
key string
value []any
// TODO 增加实时数据的topic
topic string
cancelFunc context.CancelFunc
calculatorFunc func(ctx context.Context, topic string, params []any)
lastAccessed time.Time
}
// Reset defines func to reset the CacheItem to its zero state
func (ci *CacheItem) Reset() {
ci.key = ""
ci.value = nil
if ci.cancelFunc != nil {
ci.cancelFunc()
}
ci.cancelFunc = nil
ci.calculatorFunc = nil
ci.lastAccessed = time.Time{}
}
// IsExpired defines func to check if the cache item has expired based on a given TTL
func (ci *CacheItem) IsExpired(ttl time.Duration) bool {
return time.Since(ci.lastAccessed) > ttl
}