Mensa-API/cache/cache.go
2024-12-20 00:52:24 +01:00

39 lines
716 B
Go

package cache
import (
"errors"
"github.com/LeRoid-hub/Mensa-API/models"
)
var Cache = make(map[string]models.CacheItem[interface{}])
func HasCacheData(key string) bool {
data, ok := Cache[key]
if !ok {
return false
}
if data.IsExpired() {
delete(Cache, key)
return false
}
return ok
}
func GetCacheData(key string) (interface{}, error) {
Item, ok := Cache[key]
if !ok {
return models.Mensa{}, errors.New("no data in cache")
}
return Item.GetData()
}
func SetCacheData(key string, data interface{}, lifetime ...int64) {
Item, ok := Cache[key]
if !ok {
Item = models.CacheItem[interface{}]{}
}
Item.SetData(data, lifetime...)
Cache[key] = Item
}