@@ -24,17 +24,20 @@ import (
24
24
"io"
25
25
"net/http"
26
26
"net/url"
27
+ "time"
27
28
28
29
"github.com/optimizely/agent/plugins/userprofileservice"
29
30
"github.com/optimizely/go-sdk/pkg/decision"
30
31
"github.com/optimizely/go-sdk/pkg/logging"
31
32
"github.com/optimizely/go-sdk/pkg/utils"
33
+ "github.com/patrickmn/go-cache"
32
34
"github.com/rs/zerolog/log"
33
35
)
34
36
35
37
// RestUserProfileService represents the rest API implementation of UserProfileService interface
36
38
type RestUserProfileService struct {
37
39
Requester * utils.HTTPRequester
40
+ Cache * cache.Cache
38
41
Host string `json:"host"`
39
42
Headers map [string ]string `json:"headers"`
40
43
LookupPath string `json:"lookupPath"`
@@ -58,6 +61,10 @@ func (r *RestUserProfileService) Lookup(userID string) (profile decision.UserPro
58
61
59
62
userIDKey := r .getUserIDKey ()
60
63
// Check if profile exists
64
+ cachedProfile , found := r .Cache .Get (userIDKey )
65
+ if found {
66
+ return cachedProfile .(decision.UserProfile )
67
+ }
61
68
parameters := map [string ]interface {}{userIDKey : userID }
62
69
success , response := r .performRequest (requestURL , r .LookupMethod , parameters )
63
70
if ! success {
@@ -70,7 +77,9 @@ func (r *RestUserProfileService) Lookup(userID string) (profile decision.UserPro
70
77
return
71
78
}
72
79
73
- return convertToUserProfile (userProfileMap , userIDKey )
80
+ userProfile := convertToUserProfile (userProfileMap , userIDKey )
81
+ r .Cache .Set (userProfile .ID , userProfile , cache .DefaultExpiration )
82
+ return userProfile
74
83
}
75
84
76
85
// Save is used to save bucketing decisions for users
@@ -162,10 +171,12 @@ func (r *RestUserProfileService) performRequest(requestURL, method string, param
162
171
}
163
172
164
173
func init () {
174
+ c := cache .New (5 * time .Minute , 10 * time .Minute )
165
175
restUPSCreator := func () decision.UserProfileService {
166
176
return & RestUserProfileService {
167
177
Requester : utils .NewHTTPRequester (logging .GetLogger ("" , "RestUserProfileService" )),
168
178
Headers : map [string ]string {},
179
+ Cache : c ,
169
180
}
170
181
}
171
182
userprofileservice .Add ("rest" , restUPSCreator )
0 commit comments