mirror of
https://github.com/LeRoid-hub/Mensa-API.git
synced 2025-01-30 19:24:56 +00:00
implement caching for city and state routes
This commit is contained in:
parent
9e27e076a0
commit
92ebea8506
@ -1,6 +1,7 @@
|
||||
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"
|
||||
@ -15,6 +16,18 @@ func city(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if cache.HasCacheData("city/" + city) {
|
||||
cacheData, err := cache.GetCacheData("city/" + city)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(200, cacheData)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := fetch.Fetch(city)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
@ -32,5 +45,7 @@ func city(c *gin.Context) {
|
||||
}
|
||||
|
||||
scraped := scrape.ScrapeState(resp.Body)
|
||||
|
||||
cache.SetCacheData("city/"+city, scraped)
|
||||
c.JSON(200, scraped)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
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"
|
||||
@ -15,6 +16,18 @@ func state(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if cache.HasCacheData("state/" + state) {
|
||||
cacheData, err := cache.GetCacheData("state/" + state)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(200, cacheData)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := fetch.Fetch(state + ".html")
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
@ -32,6 +45,7 @@ func state(c *gin.Context) {
|
||||
}
|
||||
|
||||
scraped := scrape.ScrapeState(resp.Body)
|
||||
cache.SetCacheData("state/"+state, scraped)
|
||||
|
||||
c.JSON(200, scraped)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user