modelRT/real-time-data/real_time_cache.go

36 lines
888 B
Go
Raw Normal View History

2025-09-17 16:41:30 +08:00
// Package realtimedata define real time data operation functions
package realtimedata
import (
"context"
2025-09-17 16:41:30 +08:00
"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)
2025-09-18 16:53:25 +08:00
lastAccessed time.Time
2025-09-17 16:41:30 +08:00
}
2025-09-18 16:53:25 +08:00
// Reset defines func to reset the CacheItem to its zero state
2025-09-17 16:41:30 +08:00
func (ci *CacheItem) Reset() {
ci.key = ""
ci.value = nil
if ci.cancelFunc != nil {
ci.cancelFunc()
}
ci.cancelFunc = nil
2025-09-18 16:53:25 +08:00
ci.calculatorFunc = nil
ci.lastAccessed = time.Time{}
2025-09-17 16:41:30 +08:00
}
2025-09-18 16:53:25 +08:00
// IsExpired defines func to check if the cache item has expired based on a given TTL
2025-09-17 16:41:30 +08:00
func (ci *CacheItem) IsExpired(ttl time.Duration) bool {
2025-09-18 16:53:25 +08:00
return time.Since(ci.lastAccessed) > ttl
2025-09-17 16:41:30 +08:00
}