-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
90 lines (73 loc) · 1.51 KB
/
cache.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
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"log/slog"
"sort"
"sync"
"time"
)
type cache struct {
keycloak *keycloak
lock sync.Mutex
state []*AccessUser
hash string
watchers map[chan struct{}]struct{}
}
func newCache(k *keycloak) *cache {
return &cache{keycloak: k, watchers: map[chan struct{}]struct{}{}}
}
func (c *cache) Fill() error {
ctx, done := context.WithTimeout(context.Background(), time.Minute)
defer done()
users, err := c.keycloak.ListUsers(ctx)
if err != nil {
return err
}
sort.Slice(users, func(i, j int) bool { return users[i].FobID < users[j].FobID })
hash := calculateUsersHash(users)
c.lock.Lock()
defer c.lock.Unlock()
if c.hash == hash {
slog.Info("cache was filled but nothing changed")
return nil // nothing has changed
}
c.state = users
c.hash = hash
for ch := range c.watchers {
select {
case ch <- struct{}{}:
default:
}
}
slog.Info("filled cache")
return nil
}
func (c *cache) Load() ([]*AccessUser, string) {
c.lock.Lock()
defer c.lock.Unlock()
return c.state, c.hash
}
func (c *cache) Wait(period time.Duration) {
ch := make(chan struct{}, 1)
c.lock.Lock()
c.watchers[ch] = struct{}{}
c.lock.Unlock()
t := time.NewTimer(period)
defer t.Stop()
select {
case <-ch:
case <-t.C:
}
c.lock.Lock()
delete(c.watchers, ch)
c.lock.Unlock()
close(ch)
}
func calculateUsersHash(users []*AccessUser) string {
js, _ := json.Marshal(&users)
hash := sha256.Sum256(js)
return hex.EncodeToString(hash[:])
}