67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package types
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// CacheType, representing the abstract value of a CacheEntity
|
|
type CacheType interface {
|
|
Weather | Metrics | Wind | Forecast
|
|
}
|
|
|
|
// CacheEntity, representing the value of the cache
|
|
type CacheEntity[T CacheType] struct {
|
|
Element T
|
|
Timestamp time.Time
|
|
}
|
|
|
|
// Cache, representing a mapping between a key(str) and a CacheEntity
|
|
type Cache[T CacheType] struct {
|
|
Data map[string]CacheEntity[T]
|
|
}
|
|
|
|
// Caches, representing a grouping of the various caches
|
|
type Caches struct {
|
|
WeatherCache Cache[Weather]
|
|
MetricsCache Cache[Metrics]
|
|
WindCache Cache[Wind]
|
|
ForecastCache Cache[Forecast]
|
|
}
|
|
|
|
func InitCache() *Caches {
|
|
return &Caches{
|
|
WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])},
|
|
MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])},
|
|
WindCache: Cache[Wind]{Data: make(map[string]CacheEntity[Wind])},
|
|
ForecastCache: Cache[Forecast]{Data: make(map[string]CacheEntity[Forecast])},
|
|
}
|
|
}
|
|
|
|
func (cache *Cache[T]) GetEntry(key string, ttl int8) (T, bool) {
|
|
val, isPresent := cache.Data[key]
|
|
|
|
// If key is not present, return a zero value
|
|
if !isPresent {
|
|
return val.Element, false
|
|
}
|
|
|
|
// Otherwise check whether cache element is expired
|
|
currentTime := time.Now()
|
|
expired := currentTime.Sub(val.Timestamp) > (time.Duration(ttl) * time.Hour)
|
|
if expired {
|
|
return val.Element, false
|
|
}
|
|
|
|
return val.Element, true
|
|
}
|
|
|
|
func (cache *Cache[T]) AddEntry(entry T, cityName string) {
|
|
currentTime := time.Now()
|
|
|
|
cache.Data[cityName] = CacheEntity[T]{
|
|
Element: entry,
|
|
Timestamp: currentTime,
|
|
}
|
|
|
|
}
|