From 42a1ce756b7a796e8ea5b7c8fc06d37d1e1f2f99 Mon Sep 17 00:00:00 2001 From: Kacper Sawicki Date: Wed, 7 Aug 2024 12:21:07 +0200 Subject: [PATCH] Add cache-ttl and short-cache-ttl to set cache expiration times --- api/cache/cache.go | 5 ++++- api/handler/account.go | 4 ++-- api/handler/circulation.go | 17 +++++++++++++++++ api/handler/epoch.go | 12 ++++++------ api/handler/handler.go | 6 ------ api/handler/layer.go | 4 ++-- api/handler/smesher.go | 14 ++++++++------ api/router/router.go | 1 + cmd/api/main.go | 16 ++++++++++++++++ 9 files changed, 56 insertions(+), 23 deletions(-) diff --git a/api/cache/cache.go b/api/cache/cache.go index 4cb29ce..9477f25 100644 --- a/api/cache/cache.go +++ b/api/cache/cache.go @@ -8,8 +8,11 @@ import ( "time" ) +var Expiration time.Duration = 0 +var ShortExpiration = 5 * time.Minute + func New() *marshaler.Marshaler { - client := gocache.New(gocache.NoExpiration, 6*time.Hour) + client := gocache.New(Expiration, 6*time.Hour) s := gocacheStore.NewGoCache(client) manager := cache.New[any](s) return marshaler.New(manager) diff --git a/api/handler/account.go b/api/handler/account.go index 5379fc5..e51ec05 100644 --- a/api/handler/account.go +++ b/api/handler/account.go @@ -4,11 +4,11 @@ import ( "context" "github.com/eko/gocache/lib/v4/store" "github.com/labstack/echo/v4" + "github.com/spacemeshos/explorer-backend/api/cache" "github.com/spacemeshos/explorer-backend/api/storage" "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/log" "net/http" - "time" ) func Account(c echo.Context) error { @@ -33,7 +33,7 @@ func Account(c echo.Context) error { } if err = cc.Cache.Set(context.Background(), "accountStats"+address, accountStats, - store.WithExpiration(1*time.Minute)); err != nil { + store.WithExpiration(cache.ShortExpiration)); err != nil { log.Warning("failed to cache account stats: %v", err) return c.NoContent(http.StatusInternalServerError) } diff --git a/api/handler/circulation.go b/api/handler/circulation.go index 5948a68..eb116f6 100644 --- a/api/handler/circulation.go +++ b/api/handler/circulation.go @@ -28,3 +28,20 @@ func Circulation(c echo.Context) error { return c.JSON(http.StatusOK, circulation) } + +func CirculationRefresh(c echo.Context) error { + cc := c.(*ApiContext) + + circulation, err := cc.StorageClient.GetCirculation(cc.Storage) + if err != nil { + log.Warning("failed to get circulation: %v", err) + return c.NoContent(http.StatusInternalServerError) + } + + if err = cc.Cache.Set(context.Background(), "circulation", circulation); err != nil { + log.Warning("failed to cache circulation: %v", err) + return c.NoContent(http.StatusInternalServerError) + } + + return c.NoContent(http.StatusOK) +} diff --git a/api/handler/epoch.go b/api/handler/epoch.go index 4cfc0ef..702e403 100644 --- a/api/handler/epoch.go +++ b/api/handler/epoch.go @@ -2,13 +2,11 @@ package handler import ( "context" - "github.com/eko/gocache/lib/v4/store" "github.com/labstack/echo/v4" "github.com/spacemeshos/explorer-backend/api/storage" "github.com/spacemeshos/go-spacemesh/log" "net/http" "strconv" - "time" ) func Epoch(c echo.Context) error { @@ -18,7 +16,8 @@ func Epoch(c echo.Context) error { return c.NoContent(http.StatusBadRequest) } - if cached, err := cc.Cache.Get(context.Background(), "epochStats"+c.Param("id"), new(*storage.EpochStats)); err == nil { + if cached, err := cc.Cache.Get(context.Background(), "epochStats"+c.Param("id"), + new(*storage.EpochStats)); err == nil { return c.JSON(http.StatusOK, cached) } @@ -28,7 +27,7 @@ func Epoch(c echo.Context) error { return c.NoContent(http.StatusInternalServerError) } - if err = cc.Cache.Set(context.Background(), "epochStats"+c.Param("id"), epochStats, store.WithExpiration(1*time.Minute)); err != nil { + if err = cc.Cache.Set(context.Background(), "epochStats"+c.Param("id"), epochStats); err != nil { log.Warning("failed to cache epoch stats: %v", err) return c.NoContent(http.StatusInternalServerError) } @@ -64,7 +63,8 @@ func EpochDecentral(c echo.Context) error { return c.NoContent(http.StatusBadRequest) } - if cached, err := cc.Cache.Get(context.Background(), "epochStatsDecentral"+c.Param("id"), new(*storage.EpochStats)); err == nil { + if cached, err := cc.Cache.Get(context.Background(), "epochStatsDecentral"+c.Param("id"), + new(*storage.EpochStats)); err == nil { return c.JSON(http.StatusOK, cached) } @@ -74,7 +74,7 @@ func EpochDecentral(c echo.Context) error { return c.NoContent(http.StatusInternalServerError) } - if err = cc.Cache.Set(context.Background(), "epochStatsDecentral"+c.Param("id"), epochStats, store.WithExpiration(1*time.Minute)); err != nil { + if err = cc.Cache.Set(context.Background(), "epochStatsDecentral"+c.Param("id"), epochStats); err != nil { log.Warning("failed to cache epoch stats: %v", err) return c.NoContent(http.StatusInternalServerError) } diff --git a/api/handler/handler.go b/api/handler/handler.go index c6ef4ef..915d4e3 100644 --- a/api/handler/handler.go +++ b/api/handler/handler.go @@ -19,12 +19,6 @@ type ApiContext struct { func GetPagination(c echo.Context) (limit, offset int64) { limit = 20 offset = 0 - //if page := c.QueryParam("limit"); page != "" { - // limit, _ = strconv.ParseInt(page, 10, 32) - // if limit <= 0 { - // limit = 0 - // } - //} if size := c.QueryParam("offset"); size != "" { offset, _ = strconv.ParseInt(size, 10, 32) if offset <= 0 { diff --git a/api/handler/layer.go b/api/handler/layer.go index 4113634..92b3111 100644 --- a/api/handler/layer.go +++ b/api/handler/layer.go @@ -4,11 +4,11 @@ import ( "context" "github.com/eko/gocache/lib/v4/store" "github.com/labstack/echo/v4" + "github.com/spacemeshos/explorer-backend/api/cache" "github.com/spacemeshos/explorer-backend/api/storage" "github.com/spacemeshos/go-spacemesh/log" "net/http" "strconv" - "time" ) func Layer(c echo.Context) error { @@ -30,7 +30,7 @@ func Layer(c echo.Context) error { } if err = cc.Cache.Set(context.Background(), "layerStats"+c.Param("id"), - layerStats, store.WithExpiration(2*time.Minute)); err != nil { + layerStats, store.WithExpiration(cache.ShortExpiration)); err != nil { log.Warning("failed to cache layer stats: %v", err) return c.NoContent(http.StatusInternalServerError) } diff --git a/api/handler/smesher.go b/api/handler/smesher.go index 661cdc5..c7ef5d7 100644 --- a/api/handler/smesher.go +++ b/api/handler/smesher.go @@ -5,12 +5,12 @@ import ( "fmt" "github.com/eko/gocache/lib/v4/store" "github.com/labstack/echo/v4" + "github.com/spacemeshos/explorer-backend/api/cache" "github.com/spacemeshos/explorer-backend/api/storage" "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/log" "net/http" "strconv" - "time" ) func Smeshers(c echo.Context) error { @@ -78,7 +78,8 @@ func SmeshersByEpoch(c echo.Context) error { return c.NoContent(http.StatusInternalServerError) } - if err = cc.Cache.Set(context.Background(), fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, limit, offset), smeshers); err != nil { + if err = cc.Cache.Set(context.Background(), + fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, limit, offset), smeshers); err != nil { log.Warning("failed to cache smeshers: %v", err) return c.NoContent(http.StatusInternalServerError) } @@ -101,9 +102,10 @@ func SmeshersByEpochRefresh(c echo.Context) error { } for i := 0; i < len(smeshers.Smeshers); i += 20 { - if err = cc.Cache.Set(context.Background(), fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, 20, i), &storage.SmesherList{ - Smeshers: smeshers.Smeshers[i : i+20], - }); err != nil { + if err = cc.Cache.Set(context.Background(), + fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, 20, i), &storage.SmesherList{ + Smeshers: smeshers.Smeshers[i : i+20], + }); err != nil { log.Warning("failed to cache smeshers: %v", err) return c.NoContent(http.StatusInternalServerError) } @@ -133,7 +135,7 @@ func Smesher(c echo.Context) error { } if err = cc.Cache.Set(context.Background(), "smesher-"+smesherId, smesher, - store.WithExpiration(10*time.Minute)); err != nil { + store.WithExpiration(cache.ShortExpiration)); err != nil { log.Warning("failed to cache smesher: %v", err) return c.NoContent(http.StatusInternalServerError) } diff --git a/api/router/router.go b/api/router/router.go index fb19565..919b73c 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -27,4 +27,5 @@ func RefreshRouter(e *echo.Echo) { g.GET("/overview", handler.OverviewRefresh) g.GET("/smeshers/:epoch", handler.SmeshersByEpochRefresh) g.GET("/smeshers", handler.SmeshersRefresh) + g.GET("/circulation", handler.CirculationRefresh) } diff --git a/cmd/api/main.go b/cmd/api/main.go index d44f081..6695abe 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -135,6 +135,22 @@ var flags = []cli.Flag{ Destination: &metricsPortFlag, EnvVars: []string{"SPACEMESH_METRICS_PORT"}, }, + &cli.DurationFlag{ + Name: "cache-ttl", + Usage: "Cache TTL for resources like overview, epochs, cumulative stats etc.", + Required: false, + Value: 0, + Destination: &cache.Expiration, + EnvVars: []string{"SPACEMESH_CACHE_TTL"}, + }, + &cli.DurationFlag{ + Name: "short-cache-ttl", + Usage: "Short Cache TTL for resources like layers, accounts etc.", + Required: false, + Value: 5 * time.Minute, + Destination: &cache.ShortExpiration, + EnvVars: []string{"SPACEMESH_SHORT_CACHE_TTL"}, + }, } func main() {