31 lines
658 B
Go
31 lines
658 B
Go
//nolint:perfsprint // Not that concerned about microoptimizations for test code
|
|
package ezcache_test
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"codeberg.org/danjones000/ezcache"
|
|
)
|
|
|
|
func Example() {
|
|
// Create a five minute cache.
|
|
cache, _ := ezcache.New(func(id uint8) (string, error) {
|
|
fmt.Println("Fetching new value for", id)
|
|
return fmt.Sprintf("%d", id), nil
|
|
}, 5*time.Minute)
|
|
|
|
val, _ := cache.Get(5)
|
|
fmt.Println("Got value", val)
|
|
val, _ = cache.Get(5)
|
|
fmt.Println("Got value", val)
|
|
val, _ = cache.Get(4)
|
|
fmt.Println("Got value", val)
|
|
|
|
// Output:
|
|
// Fetching new value for 5
|
|
// Got value 5
|
|
// Got value 5
|
|
// Fetching new value for 4
|
|
// Got value 4
|
|
}
|