forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_persistent_storage_utils.go
117 lines (97 loc) · 2.81 KB
/
user_persistent_storage_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package statsig
import (
"bytes"
"encoding/json"
"fmt"
)
type UserPersistedValues = map[string]map[string]interface{}
type userPersistentStorageUtils struct {
cache map[string]UserPersistedValues
storage IUserPersistentStorage
active bool
}
func newUserPersistentStorageUtils(options *Options) *userPersistentStorageUtils {
return &userPersistentStorageUtils{
cache: make(map[string]UserPersistedValues),
storage: options.UserPersistentStorage,
active: options.UserPersistentStorage != nil,
}
}
func (p *userPersistentStorageUtils) getUserPersistedValues(user User, idType string) UserPersistedValues {
key := getStorageKey(user, idType)
if cached, ok := p.cache[key]; ok {
return cached
}
return p.loadFromStorage(key)
}
func (p *userPersistentStorageUtils) loadFromStorage(key string) UserPersistedValues {
if !p.active {
return nil
}
logError := func(err error) {
Logger().LogError(fmt.Sprintf("Failed to load key (%s) from UserPersistentStorage (%s)\n", key, err.Error()))
}
defer func() {
if err := recover(); err != nil {
logError(toError(err))
}
}()
storageValues, exists := p.storage.Load(key)
if !exists {
return nil
}
var parsedValues UserPersistedValues
decoder := json.NewDecoder(bytes.NewBufferString(storageValues))
decoder.UseNumber()
err := decoder.Decode(&parsedValues)
if err != nil || parsedValues == nil {
if err != nil {
logError(err)
}
return nil
}
p.cache[key] = parsedValues
return p.cache[key]
}
func (p *userPersistentStorageUtils) saveToStorage(user User, idType string, userPersistedValues UserPersistedValues) {
if !p.active {
return
}
key := getStorageKey(user, idType)
logError := func(err error) {
Logger().LogError(fmt.Sprintf("Failed to save key (%s) to UserPersistentStorage (%s)\n", key, err.Error()))
}
defer func() {
if err := recover(); err != nil {
logError(toError(err))
}
}()
stringified, err := json.Marshal(userPersistedValues)
if err != nil {
logError(err)
return
}
p.storage.Save(key, string(stringified))
}
func (p *userPersistentStorageUtils) removeExperimentFromStorage(user User, idType string, configName string) {
persistedValues := p.getUserPersistedValues(user, idType)
if persistedValues != nil {
delete(persistedValues, configName)
p.saveToStorage(user, idType, persistedValues)
}
}
func (p *userPersistentStorageUtils) addEvaluationToUserPersistedValues(userPersistedValues *UserPersistedValues, configName string, evaluation *evalResult) {
if userPersistedValues == nil {
*userPersistedValues = make(UserPersistedValues)
}
(*userPersistedValues)[configName] = evaluation.toMap()
}
func getStorageKey(user User, idType string) string {
var unitID string
if idType == "userID" {
unitID = user.UserID
} else {
unitID = user.CustomIDs[idType]
}
return fmt.Sprintf("%s:%s", unitID, idType)
}