-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_cache.go
More file actions
172 lines (138 loc) · 4.08 KB
/
user_cache.go
File metadata and controls
172 lines (138 loc) · 4.08 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// A cache store (in memory) for storing user slack status against unique user id
package main
import (
"errors"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type userMeta struct {
username string
slackUserID string
status string
}
type cachedUser struct {
user userMeta
expireTimestamp int64
}
type localCache struct {
stop chan struct{}
wg sync.WaitGroup
mu sync.RWMutex
users map[string]cachedUser
}
var (
errUserNotInCache = errors.New("no_user_in_cache")
errUserExpired = errors.New("user_data_expired")
)
func newLocalCache() *localCache {
lc := &localCache{
users: make(map[string]cachedUser),
stop: make(chan struct{}),
}
lc.wg.Add(1)
go func() {
defer lc.wg.Done()
}()
return lc
}
// update: adds new cache entry otherwise udpates existing
func (lc *localCache) update(u userMeta, expireTimestamp int64) {
lc.mu.Lock()
defer lc.mu.Unlock()
lc.users[u.username] = cachedUser{
user: u,
expireTimestamp: expireTimestamp,
}
promCacheUpdate.Inc()
log.WithFields(log.Fields{"func": "update", "username": u.username}).Debug("user cache updated.")
}
func (lc *localCache) read(username string) (userMeta, error) {
lc.mu.RLock()
defer lc.mu.RUnlock()
cu, ok := lc.users[username]
if !ok {
promCacheRead.WithLabelValues("miss", "username_not_found").Inc()
log.WithFields(log.Fields{"func": "read", "cache": "miss", "reason": "username_not_found", "username": username}).Debug("miss.")
return userMeta{}, errUserNotInCache
}
t := time.Now()
ct := time.Unix(cu.expireTimestamp, 0)
if t.After(ct) {
promCacheRead.WithLabelValues("miss", "expired").Inc()
log.WithFields(log.Fields{"func": "read", "cache": "miss", "reason": "expired", "username": username}).Debug("expired.")
return cu.user, errUserExpired
}
promCacheRead.WithLabelValues("hit", "").Inc()
log.WithFields(log.Fields{"func": "read", "cache": "hit", "reason": "", "username": username}).Debug("hit.")
return cu.user, nil
}
// delete: currently does not need this and expected to keep cache forever
func (lc *localCache) delete(username string) error {
lc.mu.Lock()
defer lc.mu.Unlock()
_, ok := lc.users[username]
if !ok {
log.WithFields(log.Fields{"func": "delete", "reason": "username_not_found", "username": username}).Debug("delete failed.")
return errUserNotInCache
}
promCacheDelete.Inc()
log.WithFields(log.Fields{"func": "delete", "username": username}).Debug("delete successful.")
delete(lc.users, username)
return nil
}
// getMissingIDs: return a list of usernames if they do not have a slackUserID set in the cache
func (lc *localCache) getMissingIDs(userIDs ...string) []string {
var missingIDs []string
// No users in cache so return all
if len(lc.users) == 0 {
return userIDs
}
for _, userID := range userIDs {
for k, v := range lc.users {
if userID == k {
if v.user.slackUserID == "" {
missingIDs = append(missingIDs, userID)
}
break
}
}
}
return missingIDs
}
// clear: Clear a users status and expired time from the cache based on username
func (lc *localCache) clear(username string) error {
lc.mu.RLock()
defer lc.mu.RUnlock()
cu, ok := lc.users[username]
if !ok {
log.WithFields(log.Fields{"func": "clear", "reason": "username_not_found", "username": username}).Debug("username not found in cache.")
return errUserNotInCache
}
lc.users[username] = cachedUser{
user: userMeta{username: cu.user.username, slackUserID: cu.user.slackUserID},
}
promCacheClear.Inc()
log.WithFields(log.Fields{"func": "clear", "username": username}).Debug("user cache cleared.")
return nil
}
type userList struct {
Username string
SlackUserID string
SlackStatus string
CacheExpire time.Time
Expired bool
}
func (lc *localCache) getUserList() []userList {
ul := []userList{}
t := time.Now()
for k, v := range lc.users {
ct := time.Unix(v.expireTimestamp, 0)
var expiredTS bool
if t.After(ct) {
expiredTS = true
}
ul = append(ul, userList{Username: k, SlackUserID: v.user.slackUserID, SlackStatus: v.user.status, CacheExpire: time.Unix(v.expireTimestamp, 0), Expired: expiredTS})
}
return ul
}