Skip to content

Commit b892498

Browse files
committed
improve readability, change key generation
1 parent da41aad commit b892498

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/Masterminds/semver/v3 v3.2.1
1414
github.com/algolia/algoliasearch-client-go/v3 v3.31.1
1515
github.com/deepmap/oapi-codegen/v2 v2.1.0
16+
github.com/dgraph-io/ristretto/v2 v2.1.0
1617
github.com/getkin/kin-openapi v0.123.0
1718
github.com/golang-jwt/jwt/v5 v5.2.1
1819
github.com/google/uuid v1.6.0
@@ -54,7 +55,6 @@ require (
5455
github.com/containerd/log v0.1.0 // indirect
5556
github.com/cpuguy83/dockercfg v0.3.1 // indirect
5657
github.com/davecgh/go-spew v1.1.1 // indirect
57-
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
5858
github.com/distribution/reference v0.5.0 // indirect
5959
github.com/docker/docker v25.0.2+incompatible // indirect
6060
github.com/docker/go-connections v0.5.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRd
7575
github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8=
7676
github.com/dgraph-io/ristretto/v2 v2.1.0 h1:59LjpOJLNDULHh8MC4UaegN52lC4JnO2dITsie/Pa8I=
7777
github.com/dgraph-io/ristretto/v2 v2.1.0/go.mod h1:uejeqfYXpUomfse0+lO+13ATz4TypQYLJZzBSAemuB4=
78+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
79+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
7880
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
7981
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
8082
github.com/docker/docker v25.0.2+incompatible h1:/OaKeauroa10K4Nqavw4zlhcDq/WBcPMc5DbjOGgozY=
@@ -200,6 +202,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
200202
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
201203
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
202204
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
205+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
206+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
203207
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
204208
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
205209
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
@@ -224,6 +228,8 @@ github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.1.3 h1:+p/67kyG/ySqYgb
224228
github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.1.3/go.mod h1:ANGVlWG7HgaLvW3npXhu074jiSB4dA6HAux/fmVTvJE=
225229
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
226230
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
231+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
232+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
227233
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
228234
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
229235
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
@@ -250,6 +256,10 @@ github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
250256
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
251257
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
252258
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
259+
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
260+
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
261+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
262+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
253263
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
254264
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
255265
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=

server/implementation/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (s *DripStrictServerImplementation) ListAllNodes(
284284
if request.Params.Latest != nil {
285285
latest = *request.Params.Latest
286286
}
287-
nodeResults, err := s.RegistryService.ListNodesWCache(ctx, s.Client, page, limit, filter, latest)
287+
nodeResults, err := s.RegistryService.ListNodesWithCache(ctx, s.Client, page, limit, filter, latest)
288288
if err != nil {
289289
log.Ctx(ctx).Error().Msgf("Failed to list nodes w/ err: %v", err)
290290
return drip.ListAllNodes500JSONResponse{Message: "Failed to list nodes", Error: err.Error()}, err

services/registry/registry_svc.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dripservices
33
import (
44
"bytes"
55
"context"
6+
"crypto/md5"
7+
"encoding/hex"
68
"encoding/json"
79
"errors"
810
"fmt"
@@ -78,37 +80,65 @@ func NewRegistryService(storageSvc storage.StorageService, pubsubService pubsub.
7880
}
7981
}
8082

81-
func (s *RegistryService) ListNodesWCache(
83+
// generateCacheKey creates a UUID-based cache key from the page, limit, and filter
84+
func (s *RegistryService) generateCacheKey(page, limit int, filter *entity.NodeFilter) string {
85+
// Concatenate values into a string
86+
var filterData string
87+
if filter != nil {
88+
filterData = fmt.Sprintf(
89+
"%d-%d-%s-%s-%v-%s",
90+
page,
91+
limit,
92+
filter.PublisherID,
93+
filter.Search,
94+
filter.IncludeBanned,
95+
filter.Timestamp,
96+
)
97+
} else {
98+
filterData = fmt.Sprintf("%d-%d", page, limit)
99+
}
100+
101+
// Create a hash of the concatenated string to generate a unique cache key
102+
hash := md5.New()
103+
hash.Write([]byte(filterData))
104+
105+
// Return the hashed key as a hex string
106+
return hex.EncodeToString(hash.Sum(nil))
107+
}
108+
109+
func (s *RegistryService) ListNodesWithCache(
82110
ctx context.Context,
83111
client *ent.Client,
84112
page, limit int,
85-
filter *entity.NodeFilter, latest bool) (*entity.ListNodesResult, error) {
113+
filter *entity.NodeFilter,
114+
latest bool) (*entity.ListNodesResult, error) {
115+
// Start New Relic transaction segment
116+
tracing.TraceDefaultSegment(ctx, "RegistryService.ListNodesWithCache")()
86117

87-
key := fmt.Sprintf("ListNodes-%d-%d", page, limit)
88-
if filter != nil {
89-
key = fmt.Sprintf("%s-%s-%s", key, filter.PublisherID, filter.Search)
90-
if filter.IncludeBanned != nil {
91-
key = fmt.Sprintf("%s-%b", key, filter.IncludeBanned)
92-
}
93-
if filter.Timestamp != nil {
94-
key = fmt.Sprintf("%s-%s", key, filter.Timestamp)
95-
}
96-
}
118+
// Generate a unique hash for the cache key based on filters, page, and limit
119+
key := s.generateCacheKey(page, limit, filter)
97120

121+
// Skip cache lookup if 'latest' flag is set
98122
if !latest {
123+
// Try to retrieve from cache
99124
r, ok := s.listNodeCache.Get(key)
100125
if ok {
101126
return r, nil
102127
}
103128
}
104129

130+
// Fetch the data from the source
105131
r, err := s.ListNodes(ctx, client, page, limit, filter)
106132
if err != nil {
107133
return nil, err
108134
}
109135

136+
// Set the cache with the result, using the hash key and an appropriate TTL
110137
s.listNodeCache.SetWithTTL(key, r, int64(len(r.Nodes)), time.Minute)
138+
139+
// Ensure that cache operations are finished before returning
111140
s.listNodeCache.Wait()
141+
112142
return r, nil
113143
}
114144

0 commit comments

Comments
 (0)