Skip to content

Commit

Permalink
Add cache for enrich metadata (#98)
Browse files Browse the repository at this point in the history
* Add cache for enrich metadata

* Change key format

* Remove default annotation

* Refactor to decouple configs using options

* Fix tests

* Refactor to use decorator for the cache

* Rename struct

* Duplicated code

* Change empty config check

* Enable cloud save and enrich if config provided

* Remove unnecessary if
  • Loading branch information
miguelreiswildlife committed Sep 26, 2023
1 parent cda5bf0 commit a47754d
Show file tree
Hide file tree
Showing 19 changed files with 922 additions and 184 deletions.
25 changes: 24 additions & 1 deletion api/app.go
Expand Up @@ -12,6 +12,8 @@ package api
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
enrichercache "github.com/topfreegames/podium/leaderboard/v2/enriching/cache"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -217,8 +219,29 @@ func (app *App) loadConfiguration() error {
}

func (app *App) configureEnrichment() {
enricher := enriching.NewEnricher(app.ParsedConfig.Enrichment, app.Logger)
enricher := enriching.NewEnricher(
enriching.WithLogger(app.Logger),
enriching.WithWebhookUrls(app.ParsedConfig.Enrichment.WebhookUrls),
enriching.WithWebhookTimeout(app.ParsedConfig.Enrichment.WebhookTimeout),
enriching.WithCloudSaveUrl(app.ParsedConfig.Enrichment.CloudSave.Url),
enriching.WithCloudSaveEnabled(app.ParsedConfig.Enrichment.CloudSave.Enabled),
)
app.Enricher = enriching.NewInstrumentedEnricher(enricher, app.DDStatsD)

if app.ParsedConfig.Enrichment.Cache.Addr != "" {
redisClient := redis.NewClient(&redis.Options{
Addr: app.ParsedConfig.Enrichment.Cache.Addr,
Password: app.ParsedConfig.Enrichment.Cache.Password,
})

enrichCache := enrichercache.NewEnricherRedisCache(redisClient)
app.Enricher = enrichercache.NewCachedEnricher(
enrichCache,
app.Enricher,
enrichercache.WithLogger(app.Logger),
enrichercache.WithTTL(app.ParsedConfig.Enrichment.Cache.TTL),
)
}
}

// OnErrorHandler handles panics
Expand Down
37 changes: 35 additions & 2 deletions config/config.go
Expand Up @@ -5,10 +5,10 @@ import (
"reflect"
"strconv"
"strings"
"time"

"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/topfreegames/podium/leaderboard/v2/enriching"
)

// GetDefaultConfig configure viper to use the config file
Expand All @@ -29,7 +29,40 @@ func GetDefaultConfig(configFile string) (*viper.Viper, error) {

type (
PodiumConfig struct {
Enrichment enriching.EnrichmentConfig
Enrichment EnrichmentConfig
}

EnrichmentConfig struct {
// CloudSaveURL is the URL to call the Cloud Save service.
CloudSave CloudSaveConfig `mapstructure:"cloud_save"`

// WebhookUrls contains the necessary parameters to call a webhook for a given game.
// The key should be the game tenantID.
WebhookUrls map[string]string `mapstructure:"webhook_urls"`

// WebhookTimeout is the timeout for the webhook call.
WebhookTimeout time.Duration `mapstructure:"webhook_timeout"`

Cache Cache `mapstructure:"cache"`
}

Cache struct {
// Add is the address for the cache.
Addr string `mapstructure:"addr"`

// Password is the password for the cache.
Password string `mapstructure:"password"`

// TTL is the time to live for the cached data.
TTL time.Duration `mapstructure:"ttl"`
}

CloudSaveConfig struct {
// Enabled indicates whether the Cloud Save service should be used for enrichment for each tenant.
Enabled map[string]bool `mapstructure:"disabled"`

// URL is the URL to call the Cloud Save service.
Url string `mapstructure:"url"`
}
)

Expand Down
6 changes: 5 additions & 1 deletion config/default.yaml
Expand Up @@ -35,7 +35,11 @@ extensions:

enrichment:
webhook_urls:
cache:
ttl: 24h
addrs: ""
username: ""
webhook_timeout: 500ms
cloud_save:
url:
disabled:
enabled:
8 changes: 7 additions & 1 deletion config/local.yaml
Expand Up @@ -2,6 +2,8 @@ healthcheck:
workingText: WORKING

redis:
addrs:
- localhost:6379
host: localhost
port: 6379
password: ""
Expand Down Expand Up @@ -37,6 +39,10 @@ enrichment:
webhook_urls:
dummy_tenant_id: "localhost:8080/"
webhook_timeout: 500ms
cache:
ttl: 24h
addrs: "localhost:6739"
username: ""
cloud_save:
url: "localhost:8888/"
disabled:
enabled:
7 changes: 2 additions & 5 deletions go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/getsentry/raven-go v0.0.0-20170918144728-1452f6376ddb
github.com/go-redis/redis/v8 v8.11.5
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gosuri/uiprogress v0.0.0-20160202012259-a9f819bfc744
Expand All @@ -13,7 +14,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/newrelic/go-agent v1.11.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.11.0
github.com/onsi/gomega v1.18.1
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e
github.com/opentracing-contrib/go-stdlib v1.0.0
github.com/opentracing/opentracing-go v1.2.0
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-pg/pg v8.0.7+incompatible // indirect
github.com/go-redis/redis v6.13.2+incompatible // indirect
github.com/go-redis/redis/v8 v8.8.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uilive v0.0.0-20160202011846-efb88ccd0599 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -65,9 +65,6 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
go.opentelemetry.io/otel v0.19.0 // indirect
go.opentelemetry.io/otel/metric v0.19.0 // indirect
go.opentelemetry.io/otel/trace v0.19.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
Expand Down
29 changes: 13 additions & 16 deletions go.sum
Expand Up @@ -65,7 +65,6 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 h1:uH66TXeswKn5PW5zdZ39xEwfS9an067BirqA+P4QaLI=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down Expand Up @@ -114,8 +113,9 @@ github.com/go-pg/pg v8.0.7+incompatible h1:ty/sXL1OZLo+47KK9N8llRcmbA9tZasqbQ/OO
github.com/go-pg/pg v8.0.7+incompatible/go.mod h1:a2oXow+aFOrvwcKs3eIA0lNFmMilrxK2sOkB5NWe0vA=
github.com/go-redis/redis v6.13.2+incompatible h1:kfEWSpgBs4XmuzGg7nYPqhQejjzU9eKdIL0PmE2TtRY=
github.com/go-redis/redis v6.13.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v8 v8.8.2 h1:O/NcHqobw7SEptA0yA6up6spZVFtwE06SXM8rgLtsP8=
github.com/go-redis/redis/v8 v8.8.2/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-redis/redismock/v8 v8.11.5 h1:RJFIiua58hrBrSpXhnGX3on79AU3S271H4ZhRI1wyVo=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -151,6 +151,7 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -179,6 +180,7 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand Down Expand Up @@ -249,14 +251,16 @@ github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/onsi/gomega v1.11.0 h1:+CqWgvj0OZycCaqclBD1pxKHAU+tOkHmQIWvDHq2aug=
github.com/onsi/gomega v1.11.0/go.mod h1:azGKhqFUon9Vuj0YmTfLSmx0FUwqXYSTl5re8lQLTUg=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e h1:4cPxUYdgaGzZIT5/j0IfqOrrXmq6bG8AwvwisMXpdrg=
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e/go.mod h1:DYR5Eij8rJl8h7gblRrOZ8g0kW1umSpKqYIBTgeDtLo=
github.com/opentracing-contrib/go-stdlib v1.0.0 h1:TBS7YuVotp8myLon4Pv7BtCBzOTo1DeZCld0Z63mW2w=
Expand Down Expand Up @@ -357,14 +361,6 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/otel v0.19.0 h1:Lenfy7QHRXPZVsw/12CWpxX6d/JkrX8wrx2vO8G80Ng=
go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg=
go.opentelemetry.io/otel/metric v0.19.0 h1:dtZ1Ju44gkJkYvo+3qGqVXmf88tc+a42edOywypengg=
go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2VmNNqIsx/uIwBSc=
go.opentelemetry.io/otel/oteltest v0.19.0 h1:YVfA0ByROYqTwOxqHVZYZExzEpfZor+MU1rU+ip2v9Q=
go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA=
go.opentelemetry.io/otel/trace v0.19.0 h1:1ucYlenXIDA1OlHVLDZKX0ObXV5RLaq06DtUKz5e5zc=
go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
Expand Down Expand Up @@ -462,11 +458,11 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
Expand Down Expand Up @@ -539,6 +535,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
Expand Down
94 changes: 94 additions & 0 deletions leaderboard/enriching/cache/cache.go
@@ -0,0 +1,94 @@
package cache

import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/topfreegames/podium/leaderboard/v2/enriching"
"github.com/topfreegames/podium/leaderboard/v2/model"
"time"
)

// cacheKeyFormat is {tenantID}:{leaderboardID}:{memberID}
const cacheKeyFormat = "leaderboards-enrich-caching:%s:%s:%s"

type enricherRedisCache struct {
redis *redis.Client
}

var _ enriching.EnricherCache = &enricherRedisCache{}

func NewEnricherRedisCache(
redis *redis.Client,
) enriching.EnricherCache {
return &enricherRedisCache{
redis: redis,
}
}

func (e *enricherRedisCache) Get(
ctx context.Context,
tenantID,
leaderboardID string,
members []*model.Member,
) (map[string]map[string]string, bool, error) {
keys := getKeysFromMemberArray(tenantID, leaderboardID, members)
dataArray, err := e.redis.MGet(ctx, keys...).Result()
if err != nil {
return nil, false, fmt.Errorf("failed to get data from cacheConfig: %w", err)
}

dataMap := make(map[string]map[string]string)
for i, data := range dataArray {
if data == nil {
return nil, false, nil
}

unmarshaled := map[string]string{}
err := json.Unmarshal([]byte(data.(string)), &unmarshaled)
if err != nil {
return nil, false, fmt.Errorf("failed to unmarshal data: %w", err)
}

memberID := members[i].PublicID
dataMap[memberID] = unmarshaled
}

return dataMap, true, nil
}

func (e *enricherRedisCache) Set(
ctx context.Context,
tenantID,
leaderboardID string,
members []*model.Member,
ttl time.Duration,
) error {
keys := getKeysFromMemberArray(tenantID, leaderboardID, members)
pipe := e.redis.TxPipeline()
for i, member := range members {
if member.Metadata != nil {
marshaled, err := json.Marshal(member.Metadata)
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
}
pipe.Set(ctx, keys[i], marshaled, ttl)
}
}

_, err := pipe.Exec(ctx)
if err != nil {
return fmt.Errorf("failed to set members in cacheConfig: %w", err)
}

return nil
}

func getKeysFromMemberArray(tenantID, leaderboardID string, members []*model.Member) []string {
keys := make([]string, len(members))
for i, member := range members {
keys[i] = fmt.Sprintf(cacheKeyFormat, tenantID, leaderboardID, member.PublicID)
}
return keys
}
12 changes: 12 additions & 0 deletions leaderboard/enriching/cache/cache_suite_test.go
@@ -0,0 +1,12 @@
package cache

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func TestEnrichingCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Enriching Cache Suite")
}

0 comments on commit a47754d

Please sign in to comment.