Skip to content

Commit

Permalink
improve readability, change key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Feb 5, 2025
1 parent da41aad commit b892498
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/algolia/algoliasearch-client-go/v3 v3.31.1
github.com/deepmap/oapi-codegen/v2 v2.1.0
github.com/dgraph-io/ristretto/v2 v2.1.0
github.com/getkin/kin-openapi v0.123.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -54,7 +55,6 @@ require (
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.2+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRd
github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8=
github.com/dgraph-io/ristretto/v2 v2.1.0 h1:59LjpOJLNDULHh8MC4UaegN52lC4JnO2dITsie/Pa8I=
github.com/dgraph-io/ristretto/v2 v2.1.0/go.mod h1:uejeqfYXpUomfse0+lO+13ATz4TypQYLJZzBSAemuB4=
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v25.0.2+incompatible h1:/OaKeauroa10K4Nqavw4zlhcDq/WBcPMc5DbjOGgozY=
Expand Down Expand Up @@ -200,6 +202,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
Expand All @@ -224,6 +228,8 @@ github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.1.3 h1:+p/67kyG/ySqYgb
github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.1.3/go.mod h1:ANGVlWG7HgaLvW3npXhu074jiSB4dA6HAux/fmVTvJE=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
Expand All @@ -250,6 +256,10 @@ github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
2 changes: 1 addition & 1 deletion server/implementation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (s *DripStrictServerImplementation) ListAllNodes(
if request.Params.Latest != nil {
latest = *request.Params.Latest
}
nodeResults, err := s.RegistryService.ListNodesWCache(ctx, s.Client, page, limit, filter, latest)
nodeResults, err := s.RegistryService.ListNodesWithCache(ctx, s.Client, page, limit, filter, latest)
if err != nil {
log.Ctx(ctx).Error().Msgf("Failed to list nodes w/ err: %v", err)
return drip.ListAllNodes500JSONResponse{Message: "Failed to list nodes", Error: err.Error()}, err
Expand Down
54 changes: 42 additions & 12 deletions services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dripservices
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -78,37 +80,65 @@ func NewRegistryService(storageSvc storage.StorageService, pubsubService pubsub.
}
}

func (s *RegistryService) ListNodesWCache(
// generateCacheKey creates a UUID-based cache key from the page, limit, and filter
func (s *RegistryService) generateCacheKey(page, limit int, filter *entity.NodeFilter) string {
// Concatenate values into a string
var filterData string
if filter != nil {
filterData = fmt.Sprintf(
"%d-%d-%s-%s-%v-%s",
page,
limit,
filter.PublisherID,
filter.Search,
filter.IncludeBanned,
filter.Timestamp,
)
} else {
filterData = fmt.Sprintf("%d-%d", page, limit)
}

// Create a hash of the concatenated string to generate a unique cache key
hash := md5.New()
hash.Write([]byte(filterData))

// Return the hashed key as a hex string
return hex.EncodeToString(hash.Sum(nil))
}

func (s *RegistryService) ListNodesWithCache(
ctx context.Context,
client *ent.Client,
page, limit int,
filter *entity.NodeFilter, latest bool) (*entity.ListNodesResult, error) {
filter *entity.NodeFilter,
latest bool) (*entity.ListNodesResult, error) {
// Start New Relic transaction segment
tracing.TraceDefaultSegment(ctx, "RegistryService.ListNodesWithCache")()

key := fmt.Sprintf("ListNodes-%d-%d", page, limit)
if filter != nil {
key = fmt.Sprintf("%s-%s-%s", key, filter.PublisherID, filter.Search)
if filter.IncludeBanned != nil {
key = fmt.Sprintf("%s-%b", key, filter.IncludeBanned)
}
if filter.Timestamp != nil {
key = fmt.Sprintf("%s-%s", key, filter.Timestamp)
}
}
// Generate a unique hash for the cache key based on filters, page, and limit
key := s.generateCacheKey(page, limit, filter)

// Skip cache lookup if 'latest' flag is set
if !latest {
// Try to retrieve from cache
r, ok := s.listNodeCache.Get(key)
if ok {
return r, nil
}
}

// Fetch the data from the source
r, err := s.ListNodes(ctx, client, page, limit, filter)
if err != nil {
return nil, err
}

// Set the cache with the result, using the hash key and an appropriate TTL
s.listNodeCache.SetWithTTL(key, r, int64(len(r.Nodes)), time.Minute)

// Ensure that cache operations are finished before returning
s.listNodeCache.Wait()

return r, nil
}

Expand Down

0 comments on commit b892498

Please sign in to comment.