Skip to content

Commit

Permalink
cache location response
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Dec 8, 2024
1 parent 96ddfc0 commit b5a3940
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/zos_api/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@ package zosapi
import (
"context"

"github.com/patrickmn/go-cache"
"github.com/threefoldtech/zos/pkg/geoip"
)

const (
locationCacheKey = "location"
)

func (g *ZosAPI) locationGet(ctx context.Context, payload []byte) (interface{}, error) {
return geoip.Fetch()
if loc, found := g.inMemCache.Get(locationCacheKey); found {
return loc, nil
}

loc, err := geoip.Fetch()
if err != nil {
return nil, err
}

g.inMemCache.Set(locationCacheKey, loc, cache.DefaultExpiration)

return loc, nil
}
9 changes: 9 additions & 0 deletions pkg/zos_api/zos_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package zosapi

import (
"fmt"
"time"

"github.com/patrickmn/go-cache"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/capacity"
Expand All @@ -11,6 +13,11 @@ import (
"github.com/threefoldtech/zos/pkg/stubs"
)

const (
cacheDefaultExpiration = 24 * time.Hour
cacheDefaultCleanup = 24 * time.Hour
)

type ZosAPI struct {
oracle *capacity.ResourceOracle
versionMonitorStub *stubs.VersionMonitorStub
Expand All @@ -22,6 +29,7 @@ type ZosAPI struct {
performanceMonitorStub *stubs.PerformanceMonitorStub
diagnosticsManager *diagnostics.DiagnosticsManager
farmerID uint32
inMemCache *cache.Cache
}

func NewZosAPI(manager substrate.Manager, client zbus.Client, msgBrokerCon string) (ZosAPI, error) {
Expand Down Expand Up @@ -56,5 +64,6 @@ func NewZosAPI(manager substrate.Manager, client zbus.Client, msgBrokerCon strin
return ZosAPI{}, err
}
api.farmerID = uint32(farmer.ID)
api.inMemCache = cache.New(cacheDefaultExpiration, cacheDefaultCleanup)
return api, nil
}

0 comments on commit b5a3940

Please sign in to comment.