cache for mensa

This commit is contained in:
Jan 2024-09-15 21:39:17 +02:00
parent a5bb066df0
commit c036ce104b
5 changed files with 26 additions and 25 deletions

8
cache/cache.go vendored
View File

@ -1,6 +1,8 @@
package cache package cache
import ( import (
"errors"
"github.com/LeRoid-hub/Mensa-API/models" "github.com/LeRoid-hub/Mensa-API/models"
) )
@ -11,15 +13,15 @@ func HasCacheData(key string) bool {
return ok return ok
} }
func GetCacheData(key string) string { func GetCacheData(key string) (models.Mensa, error) {
Item, ok := Cache[key] Item, ok := Cache[key]
if !ok { if !ok {
return "" return models.Mensa{}, errors.New("no data in cache")
} }
return Item.GetData() return Item.GetData()
} }
func SetCacheData(key string, data string, lifetime ...int64) { func SetCacheData(key string, data models.Mensa, lifetime ...int64) {
Item, ok := Cache[key] Item, ok := Cache[key]
if !ok { if !ok {
Item = models.CacheItem{} Item = models.CacheItem{}

View File

@ -1,14 +1,17 @@
package models package models
import "time" import (
"errors"
"time"
)
type CacheItem struct { type CacheItem struct {
data string data Mensa
lastUpdated time.Time lastUpdated time.Time
lifetime int64 lifetime int64
} }
func (c *CacheItem) SetData(data string, lifetime ...int64) { func (c *CacheItem) SetData(data Mensa, lifetime ...int64) {
if len(lifetime) > 0 { if len(lifetime) > 0 {
c.lifetime = lifetime[0] c.lifetime = lifetime[0]
} else { } else {
@ -19,14 +22,14 @@ func (c *CacheItem) SetData(data string, lifetime ...int64) {
c.lastUpdated = time.Now() c.lastUpdated = time.Now()
} }
func (c *CacheItem) GetData() string { func (c *CacheItem) GetData() (Mensa, error) {
if time.Now().Unix()-c.lastUpdated.Unix() > c.lifetime { if time.Now().Unix()-c.lastUpdated.Unix() > c.lifetime {
return "" return Mensa{}, errors.New("cache expired")
} }
if c.data == "" { if c.lastUpdated.IsZero() {
return "" return Mensa{}, errors.New("no data in cache")
} }
return c.data return c.data, nil
} }

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"github.com/LeRoid-hub/Mensa-API/cache"
"github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/fetch"
"github.com/LeRoid-hub/Mensa-API/scrape" "github.com/LeRoid-hub/Mensa-API/scrape"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -16,11 +15,6 @@ func city(c *gin.Context) {
return return
} }
if cache.HasCacheData(city) {
c.JSON(200, cache.GetCacheData(city))
return
}
resp, err := fetch.Fetch(city) resp, err := fetch.Fetch(city)
if err != nil { if err != nil {
c.JSON(500, gin.H{ c.JSON(500, gin.H{

View File

@ -24,7 +24,14 @@ func mensa(c *gin.Context) {
} }
if cache.HasCacheData(city + "/" + mensa) { 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 return
} }
@ -46,7 +53,7 @@ func mensa(c *gin.Context) {
scraped := scrape.ScrapeMensa(resp.Body) scraped := scrape.ScrapeMensa(resp.Body)
//cache.SetCacheData(city+"/"+mensa, scraped) cache.SetCacheData(city+"/"+mensa, scraped)
c.JSON(200, scraped) c.JSON(200, scraped)
} }

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"github.com/LeRoid-hub/Mensa-API/cache"
"github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/fetch"
"github.com/LeRoid-hub/Mensa-API/scrape" "github.com/LeRoid-hub/Mensa-API/scrape"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -16,11 +15,6 @@ func state(c *gin.Context) {
return return
} }
if cache.HasCacheData(state) {
c.JSON(200, cache.GetCacheData(state))
return
}
resp, err := fetch.Fetch(state + ".html") resp, err := fetch.Fetch(state + ".html")
if err != nil { if err != nil {
c.JSON(500, gin.H{ c.JSON(500, gin.H{
@ -38,5 +32,6 @@ func state(c *gin.Context) {
} }
scraped := scrape.ScrapeState(resp.Body) scraped := scrape.ScrapeState(resp.Body)
c.JSON(200, scraped) c.JSON(200, scraped)
} }