modelRT/real-time-data/real_time_cache.go

30 lines
768 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 (
"time"
)
// CacheItem define structure for caching real time data with calculation capabilities
type CacheItem struct {
key string
value []any
2025-09-18 16:53:25 +08:00
noticeChan chan struct{}
calculatorFunc func(done chan struct{}, params []any)
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
2025-09-18 16:53:25 +08:00
ci.noticeChan = nil
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
}