-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
50 lines (45 loc) · 925 Bytes
/
hash.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
/**
* @Package brickset
* @Time: 2022/2/8 11:17 PM
* @Author: wuhb
* @File: hash.go
*/
package brickset
import (
"context"
"fmt"
"sync"
"time"
)
type sHash struct {
auth IBrickAuth
storage IBrickStorage
expires time.Duration
lock *sync.RWMutex
}
func (s *sHash) GetHash(ctx context.Context, username string) (string, error) {
v, err := s.storage.Get(ctx, fmt.Sprintf("%s-hash", username))
if err == nil {
return v.(string), nil
}
s.lock.Lock()
defer s.lock.Unlock()
h, err := s.auth.Login(ctx)
if err != nil {
return "", err
}
if err = s.storage.Set(ctx, fmt.Sprintf("%s-hash", username), h, s.expires); err != nil {
return "", err
}
return h, nil
}
func NewHash(auth IBrickAuth, store IBrickStorage, expires time.Duration) IBrickHash {
if expires <= 0 {
expires = time.Hour
}
return &sHash{
auth: auth,
storage: store,
expires: expires,
lock: new(sync.RWMutex)}
}