@@ -2,6 +2,7 @@ package storage
2
2
3
3
import (
4
4
"context"
5
+ "encoding/hex"
5
6
"fmt"
6
7
"net"
7
8
@@ -16,6 +17,8 @@ type UpdateStatusesArgs struct {
16
17
Counters StatusTx
17
18
}
18
19
20
+ var bcryptCache = make (map [string ]bool )
21
+
19
22
// UpdateStatuses set online user fields, with any device's MAC equal to one
20
23
// of addresses from given slice, to true and writes them to database.
21
24
func UpdateStatuses (ctx context.Context , args UpdateStatusesArgs ) error {
@@ -30,9 +33,21 @@ func UpdateStatuses(ctx context.Context, args UpdateStatusesArgs) error {
30
33
31
34
for _ , address := range args .Addresses {
32
35
for _ , device := range devices {
33
- if err := bcrypt .CompareHashAndPassword (device .MAC , address ); err == nil {
36
+ var matched bool
37
+ cacheKey := generateCacheKey (device .MAC , address )
38
+
39
+ if val , ok := bcryptCache [cacheKey ]; ok {
40
+ matched = val
41
+ } else {
42
+ err := bcrypt .CompareHashAndPassword (device .MAC , address )
43
+ matched = err == nil
44
+ bcryptCache [cacheKey ] = matched
45
+ }
46
+
47
+ if matched {
34
48
known += 1
35
49
onlineIDs = append (onlineIDs , device .OwnerID )
50
+ break
36
51
}
37
52
}
38
53
}
@@ -56,3 +71,7 @@ func UpdateStatuses(ctx context.Context, args UpdateStatusesArgs) error {
56
71
return nil
57
72
})
58
73
}
74
+
75
+ func generateCacheKey (mac []byte , address net.HardwareAddr ) string {
76
+ return string (mac ) + ":" + hex .EncodeToString (address )
77
+ }
0 commit comments