...
1 package server
2
3 import (
4 "sync"
5 "time"
6 )
7
8 var (
9 excessiveLock sync.Mutex
10 usercache = make(map[string]*UserCache)
11 )
12
13 func addUserToCache(token string, id string) {
14 uc := UserCache{UserID: id, created: time.Now()}
15 excessiveLock.Lock()
16 usercache[token] = &uc
17 excessiveLock.Unlock()
18 }
19
20 func getUserFromCacheByToken(token string) *UserCache {
21 excessiveLock.Lock()
22 res := usercache[token]
23 excessiveLock.Unlock()
24 return res
25 }
26
27
28 func getUserFromCache(token string) string {
29 uc := getUserFromCacheByToken(token)
30 if uc == nil {
31 return ""
32 }
33 if time.Since(uc.created) > (time.Minute * 5) {
34 return ""
35 }
36 return uc.UserID
37
38 }
39
View as plain text