Skip to content

Commit f95c95d

Browse files
committed
adds cache to ups lookup call
1 parent 9825f2d commit f95c95d

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ require (
3838
github.com/golang/protobuf v1.5.3 // indirect
3939
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
4040
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
41+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
4142
github.com/prometheus/client_model v0.2.0 // indirect
4243
github.com/prometheus/common v0.30.0 // indirect
4344
github.com/prometheus/procfs v0.7.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ github.com/optimizely/go-sdk v1.8.4-0.20230911163718-b10e161e39b8 h1:1LhZsu7IB7L
248248
github.com/optimizely/go-sdk v1.8.4-0.20230911163718-b10e161e39b8/go.mod h1:zITWqffjOXsae/Z0PlCN5kgJRgJF/0g/k8RBEsxNrxg=
249249
github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HDbW65HOY=
250250
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
251+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
252+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
251253
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
252254
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
253255
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

plugins/userprofileservice/services/rest_ups.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ import (
2424
"io"
2525
"net/http"
2626
"net/url"
27+
"time"
2728

2829
"github.com/optimizely/agent/plugins/userprofileservice"
2930
"github.com/optimizely/go-sdk/pkg/decision"
3031
"github.com/optimizely/go-sdk/pkg/logging"
3132
"github.com/optimizely/go-sdk/pkg/utils"
33+
"github.com/patrickmn/go-cache"
3234
"github.com/rs/zerolog/log"
3335
)
3436

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

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

73-
return convertToUserProfile(userProfileMap, userIDKey)
80+
userProfile := convertToUserProfile(userProfileMap, userIDKey)
81+
r.Cache.Set(userProfile.ID, userProfile, cache.DefaultExpiration)
82+
return userProfile
7483
}
7584

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

164173
func init() {
174+
c := cache.New(5*time.Minute, 10*time.Minute)
165175
restUPSCreator := func() decision.UserProfileService {
166176
return &RestUserProfileService{
167177
Requester: utils.NewHTTPRequester(logging.GetLogger("", "RestUserProfileService")),
168178
Headers: map[string]string{},
179+
Cache: c,
169180
}
170181
}
171182
userprofileservice.Add("rest", restUPSCreator)

0 commit comments

Comments
 (0)