...

Source file src/github.com/okta/okta-jwt-verifier-golang/utils/cache_example_test.go

Documentation: github.com/okta/okta-jwt-verifier-golang/utils

     1  package utils_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	jwtverifier "github.com/okta/okta-jwt-verifier-golang"
     7  	"github.com/okta/okta-jwt-verifier-golang/utils"
     8  )
     9  
    10  // ForeverCache caches values forever
    11  type ForeverCache struct {
    12  	values map[string]interface{}
    13  	lookup func(string) (interface{}, error)
    14  }
    15  
    16  // Get returns the value for the given key
    17  func (c *ForeverCache) Get(key string) (interface{}, error) {
    18  	value, ok := c.values[key]
    19  	if ok {
    20  		return value, nil
    21  	}
    22  	value, err := c.lookup(key)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	c.values[key] = value
    27  	return value, nil
    28  }
    29  
    30  // ForeverCache implements the read-only Cacher interface
    31  var _ utils.Cacher = (*ForeverCache)(nil)
    32  
    33  // NewForeverCache takes a lookup function and returns a cache
    34  func NewForeverCache(lookup func(string) (interface{}, error)) (utils.Cacher, error) {
    35  	return &ForeverCache{
    36  		values: map[string]interface{}{},
    37  		lookup: lookup,
    38  	}, nil
    39  }
    40  
    41  // Example demonstrating how the JwtVerifier can be configured with a custom Cache function.
    42  func Example() {
    43  	jwtVerifierSetup := jwtverifier.JwtVerifier{
    44  		Cache: NewForeverCache,
    45  		// other fields here
    46  	}
    47  
    48  	verifier := jwtVerifierSetup.New()
    49  	fmt.Println(verifier)
    50  }
    51  

View as plain text