Skip to content

[FSSDK-9841] adds cache to ups lookup call #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -248,6 +248,8 @@ github.com/optimizely/go-sdk v1.8.4-0.20230911163718-b10e161e39b8 h1:1LhZsu7IB7L
github.com/optimizely/go-sdk v1.8.4-0.20230911163718-b10e161e39b8/go.mod h1:zITWqffjOXsae/Z0PlCN5kgJRgJF/0g/k8RBEsxNrxg=
github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HDbW65HOY=
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
13 changes: 12 additions & 1 deletion plugins/userprofileservice/services/rest_ups.go
Original file line number Diff line number Diff line change
@@ -24,17 +24,20 @@ import (
"io"
"net/http"
"net/url"
"time"

"github.com/optimizely/agent/plugins/userprofileservice"
"github.com/optimizely/go-sdk/pkg/decision"
"github.com/optimizely/go-sdk/pkg/logging"
"github.com/optimizely/go-sdk/pkg/utils"
"github.com/patrickmn/go-cache"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found this: "go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine.".

When the agent will be run with multiple replicas, this might not help. So, I think, this needs to be configurable. And by default, the caching should be disabled. Users have to manually enable it according to the underlying system. WDYT?

"github.com/rs/zerolog/log"
)

// RestUserProfileService represents the rest API implementation of UserProfileService interface
type RestUserProfileService struct {
Requester *utils.HTTPRequester
Cache *cache.Cache
Host string `json:"host"`
Headers map[string]string `json:"headers"`
LookupPath string `json:"lookupPath"`
@@ -58,6 +61,10 @@ func (r *RestUserProfileService) Lookup(userID string) (profile decision.UserPro

userIDKey := r.getUserIDKey()
// Check if profile exists
cachedProfile, found := r.Cache.Get(userIDKey)
if found {
return cachedProfile.(decision.UserProfile)
}
parameters := map[string]interface{}{userIDKey: userID}
success, response := r.performRequest(requestURL, r.LookupMethod, parameters)
if !success {
@@ -70,7 +77,9 @@ func (r *RestUserProfileService) Lookup(userID string) (profile decision.UserPro
return
}

return convertToUserProfile(userProfileMap, userIDKey)
userProfile := convertToUserProfile(userProfileMap, userIDKey)
r.Cache.Set(userProfile.ID, userProfile, cache.DefaultExpiration)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expiration time may vary user to user. So, it can be configurable.

return userProfile
}

// Save is used to save bucketing decisions for users
@@ -162,10 +171,12 @@ func (r *RestUserProfileService) performRequest(requestURL, method string, param
}

func init() {
c := cache.New(5*time.Minute, 10*time.Minute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use constants with a relevant name for those time values?

restUPSCreator := func() decision.UserProfileService {
return &RestUserProfileService{
Requester: utils.NewHTTPRequester(logging.GetLogger("", "RestUserProfileService")),
Headers: map[string]string{},
Cache: c,
}
}
userprofileservice.Add("rest", restUPSCreator)