forked from okta/okta-jwt-verifier-golang
/
cache_example_test.go
54 lines (45 loc) · 1.32 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
52
53
54
package utils_test
// Modified by Lizaveta Kemerava, April 20, 2023: including custom client
import (
"fmt"
"net/http"
"time"
jwtverifier "github.com/kemerava/okta-jwt-verifier-golang"
"github.com/kemerava/okta-jwt-verifier-golang/utils"
)
// ForeverCache caches values forever
type ForeverCache struct {
values map[string]interface{}
lookup func(string, *http.Client) (interface{}, error)
}
// Get returns the value for the given key
func (c *ForeverCache) Get(key string, client *http.Client) (interface{}, error) {
value, ok := c.values[key]
if ok {
return value, nil
}
value, err := c.lookup(key, client)
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, *http.Client) (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)
}