-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
107 lines (89 loc) · 2.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package keycloak
import (
"fmt"
"strings"
"sync"
"time"
"github.com/dcarbone/sclg/v3"
"github.com/google/go-cmp/cmp"
)
type CacheBackend interface {
Load(key interface{}) (value interface{}, ok bool)
StoreUntil(key, value interface{}, deadline time.Time)
Delete(key interface{})
Flush() int
}
var (
globalCache *sclg.TimedCache
)
func init() {
conf := new(sclg.TimedCacheConfig)
conf.Comparator = globalCacheEquivalencyTest
globalCache = sclg.NewTimedCache(conf)
}
type noopCacheImpl struct{}
var noopCacheInst = noopCacheImpl{}
// NewNoopCache will return to you a cache instance that will entirely disable all caching within the client. Not
// recommended for production use
func NewNoopCache() CacheBackend {
return noopCacheInst
}
func (noopCacheImpl) Load(_ interface{}) (interface{}, bool) { return nil, false }
func (noopCacheImpl) StoreUntil(_, _ interface{}, _ time.Time) {}
func (noopCacheImpl) Delete(_ interface{}) {}
func (noopCacheImpl) Flush() int { return 0 }
type persistentCacheImpl struct {
*sync.Map
}
// NewPersistentCache returns a CacheBackend implementation that stores items indefinitely until explicitly deleted. Not
// recommended for production use.
func NewPersistentCache() CacheBackend {
cb := new(persistentCacheImpl)
cb.Map = new(sync.Map)
return cb
}
func (p *persistentCacheImpl) StoreUntil(key, value interface{}, _ time.Time) {
p.Store(key, value)
}
func (p *persistentCacheImpl) Flush() int {
cnt := 0
p.Range(func(key, _ interface{}) bool {
p.Delete(key)
cnt++
return true
})
return cnt
}
func globalCacheEquivalencyTest(_, current, new interface{}) bool {
return cmp.Equal(current, new)
}
// buildPKCacheKey creates the public key cache entry keys.
func buildPKCacheKey(authServerURL, realm, keyID string) string {
return fmt.Sprintf(pkKeyFormat, authServerURL, realm, keyID)
}
// parsePKCacheKey splits a cache key into authServerURL : realm : keyID
func parsePKCacheKey(key interface{}) (string, string, string) {
str, ok := key.(string)
if !ok {
return "", "", ""
}
s := strings.SplitN("\n", str, 4)
if len(s) != 4 || s[0] != pkKeyPrefix {
return "", "", ""
}
return s[1], s[2], s[3]
}
func buildRealmEnvCacheKey(authServerURL, realm string) string {
return fmt.Sprintf(reKeyFormat, authServerURL, realm)
}
func parseRealmEnvCacheKey(key interface{}) (string, string) {
str, ok := key.(string)
if !ok {
return "", ""
}
s := strings.SplitN(str, "\n", 3)
if len(s) != 3 || s[0] != reKeyPrefix {
return "", ""
}
return s[1], s[2]
}