From f95c95d4c707886263b8835dd5317d3884d2ee07 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 17 Nov 2023 18:06:21 +0100 Subject: [PATCH] adds cache to ups lookup call --- go.mod | 1 + go.sum | 2 ++ plugins/userprofileservice/services/rest_ups.go | 13 ++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fcb03a04..2c7e99a2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ecd97249..df7a144a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/plugins/userprofileservice/services/rest_ups.go b/plugins/userprofileservice/services/rest_ups.go index de4d58fb..674e5024 100644 --- a/plugins/userprofileservice/services/rest_ups.go +++ b/plugins/userprofileservice/services/rest_ups.go @@ -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" "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) + 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) restUPSCreator := func() decision.UserProfileService { return &RestUserProfileService{ Requester: utils.NewHTTPRequester(logging.GetLogger("", "RestUserProfileService")), Headers: map[string]string{}, + Cache: c, } } userprofileservice.Add("rest", restUPSCreator)