Skip to content

Commit

Permalink
Add cache-ttl and short-cache-ttl to set cache expiration times
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Aug 7, 2024
1 parent c3941b1 commit 42a1ce7
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 23 deletions.
5 changes: 4 additions & 1 deletion api/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions api/handler/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
17 changes: 17 additions & 0 deletions api/handler/circulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions api/handler/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
Expand Down
6 changes: 0 additions & 6 deletions api/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions api/handler/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
14 changes: 8 additions & 6 deletions api/handler/smesher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 42a1ce7

Please sign in to comment.