From c036ce104b1add6f056d10db3b254bdfd910c2a1 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 15 Sep 2024 21:39:17 +0200 Subject: [PATCH] cache for mensa --- cache/cache.go | 8 +++++--- models/cacheItem.go | 19 +++++++++++-------- server/city.go | 6 ------ server/mensa.go | 11 +++++++++-- server/state.go | 7 +------ 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 3f415aa..1564a98 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,8 @@ package cache import ( + "errors" + "github.com/LeRoid-hub/Mensa-API/models" ) @@ -11,15 +13,15 @@ func HasCacheData(key string) bool { return ok } -func GetCacheData(key string) string { +func GetCacheData(key string) (models.Mensa, error) { Item, ok := Cache[key] if !ok { - return "" + return models.Mensa{}, errors.New("no data in cache") } return Item.GetData() } -func SetCacheData(key string, data string, lifetime ...int64) { +func SetCacheData(key string, data models.Mensa, lifetime ...int64) { Item, ok := Cache[key] if !ok { Item = models.CacheItem{} diff --git a/models/cacheItem.go b/models/cacheItem.go index 8a2f9de..12ac6b0 100644 --- a/models/cacheItem.go +++ b/models/cacheItem.go @@ -1,14 +1,17 @@ package models -import "time" +import ( + "errors" + "time" +) type CacheItem struct { - data string + data Mensa lastUpdated time.Time lifetime int64 } -func (c *CacheItem) SetData(data string, lifetime ...int64) { +func (c *CacheItem) SetData(data Mensa, lifetime ...int64) { if len(lifetime) > 0 { c.lifetime = lifetime[0] } else { @@ -19,14 +22,14 @@ func (c *CacheItem) SetData(data string, lifetime ...int64) { c.lastUpdated = time.Now() } -func (c *CacheItem) GetData() string { +func (c *CacheItem) GetData() (Mensa, error) { if time.Now().Unix()-c.lastUpdated.Unix() > c.lifetime { - return "" + return Mensa{}, errors.New("cache expired") } - if c.data == "" { - return "" + if c.lastUpdated.IsZero() { + return Mensa{}, errors.New("no data in cache") } - return c.data + return c.data, nil } diff --git a/server/city.go b/server/city.go index 5c3738b..44a1344 100644 --- a/server/city.go +++ b/server/city.go @@ -1,7 +1,6 @@ package server import ( - "github.com/LeRoid-hub/Mensa-API/cache" "github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/scrape" "github.com/gin-gonic/gin" @@ -16,11 +15,6 @@ func city(c *gin.Context) { return } - if cache.HasCacheData(city) { - c.JSON(200, cache.GetCacheData(city)) - return - } - resp, err := fetch.Fetch(city) if err != nil { c.JSON(500, gin.H{ diff --git a/server/mensa.go b/server/mensa.go index 3ed93d4..baa4d5f 100644 --- a/server/mensa.go +++ b/server/mensa.go @@ -24,7 +24,14 @@ func mensa(c *gin.Context) { } if cache.HasCacheData(city + "/" + mensa) { - c.JSON(200, cache.GetCacheData(mensa)) + cacheData, err := cache.GetCacheData(city + "/" + mensa) + if err != nil { + c.JSON(500, gin.H{ + "error": err.Error(), + }) + return + } + c.JSON(200, cacheData) return } @@ -46,7 +53,7 @@ func mensa(c *gin.Context) { scraped := scrape.ScrapeMensa(resp.Body) - //cache.SetCacheData(city+"/"+mensa, scraped) + cache.SetCacheData(city+"/"+mensa, scraped) c.JSON(200, scraped) } diff --git a/server/state.go b/server/state.go index 4d9f4f7..f6beb8f 100644 --- a/server/state.go +++ b/server/state.go @@ -1,7 +1,6 @@ package server import ( - "github.com/LeRoid-hub/Mensa-API/cache" "github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/scrape" "github.com/gin-gonic/gin" @@ -16,11 +15,6 @@ func state(c *gin.Context) { return } - if cache.HasCacheData(state) { - c.JSON(200, cache.GetCacheData(state)) - return - } - resp, err := fetch.Fetch(state + ".html") if err != nil { c.JSON(500, gin.H{ @@ -38,5 +32,6 @@ func state(c *gin.Context) { } scraped := scrape.ScrapeState(resp.Body) + c.JSON(200, scraped) }