in api.go [39:79]
func (h apiHandlers) getStats(c *gin.Context) {
cacheValue, _ := c.Get(cache.CACHE_MIDDLEWARE_KEY)
cache := *cacheValue.(*persistence.CacheStore)
const cacheKey = "shop-stats"
var stats *Stats
err := cache.Get(cacheKey, &stats)
switch err {
case nil:
contextLogger(c).Debug("serving stats from cache")
c.JSON(http.StatusOK, stats)
if tx := apm.TransactionFromContext(c.Request.Context()); tx != nil {
tx.Context.SetLabel("served_from_cache", "true")
}
return
case persistence.ErrCacheMiss:
// fetch and cache below
if tx := apm.TransactionFromContext(c.Request.Context()); tx != nil {
tx.Context.SetLabel("served_from_cache", "false")
}
break
default:
err := errors.Wrap(err, "failed to get stats from cache")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
stats, err = getStats(c.Request.Context(), h.db)
if err != nil {
err := errors.Wrap(err, "failed to query stats")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := cache.Set(cacheKey, stats, time.Minute); err != nil {
err := errors.Wrap(err, "failed to cache stats")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
contextLogger(c).Debug("cached stats")
c.JSON(http.StatusOK, stats)
}