forked from okta/okta-jwt-verifier-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_example_test.go
51 lines (43 loc) · 1.18 KB
/
cache_example_test.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
package utils_test
import (
"fmt"
"time"
jwtverifier "github.com/okta/okta-jwt-verifier-golang/v2"
"github.com/okta/okta-jwt-verifier-golang/v2/utils"
)
// ForeverCache caches values forever
type ForeverCache struct {
values map[string]interface{}
lookup func(string) (interface{}, error)
}
// Get returns the value for the given key
func (c *ForeverCache) Get(key string) (interface{}, error) {
value, ok := c.values[key]
if ok {
return value, nil
}
value, err := c.lookup(key)
if err != nil {
return nil, err
}
c.values[key] = value
return value, nil
}
// ForeverCache implements the read-only Cacher interface
var _ utils.Cacher = (*ForeverCache)(nil)
// NewForeverCache takes a lookup function and returns a cache
func NewForeverCache(lookup func(string) (interface{}, error), t, c time.Duration) (utils.Cacher, error) {
return &ForeverCache{
values: map[string]interface{}{},
lookup: lookup,
}, nil
}
// Example demonstrating how the JwtVerifier can be configured with a custom Cache function.
func Example() {
jwtVerifierSetup := jwtverifier.JwtVerifier{
Cache: NewForeverCache,
// other fields here
}
verifier, _ := jwtVerifierSetup.New()
fmt.Println(verifier)
}