Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is an error returned when the key is not found. ErrNotFound = errors.New("memo: not found") // ErrInvalidExpiration represents an invalid expiration error. ErrInvalidExpiration = errors.New("memo: invalid expiration") )
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface { // Now returns the current time in nanoseconds. Now() int64 }
A Clock represents the passage of time, it can provide the current time in nanoseconds, which could be a relative value, not an absolute value.
type GetOption ¶
type GetOption[K comparable, V any] func(*getOptions[K, V])
GetOption specifies the option when getting a value from the memo.
func GetWithExpiration ¶
func GetWithExpiration[K comparable, V any](expiration time.Duration) GetOption[K, V]
GetWithExpiration provides an expiration option when getting a value from the memo.
func GetWithLoader ¶
func GetWithLoader[K comparable, V any](loader Loader[K, V]) GetOption[K, V]
GetWithLoader provides a loader option when getting a value from the memo.
type Loader ¶
type Loader[K comparable, V any] func(K) (V, error)
A Loader returns the value of the key.
Example ¶
package main import ( "errors" "fmt" "github.com/rbee3u/golib/memo" ) func main() { fmt.Println("Scene: default loader only") m := memo.New(memo.WithLoader(length)) fmt.Println(m.Get("x")) fmt.Println(m.Get("error")) fmt.Println("Scene: get loader only") m = memo.New[string, int]() fmt.Println(m.Get("x", memo.GetWithLoader(length))) fmt.Println(m.Get("error", memo.GetWithLoader(length))) fmt.Println("Scene: get loader overwrites default loader") m = memo.New(memo.WithLoader(length)) fmt.Println(m.Get("x", memo.GetWithLoader(doubleLength))) } func length(k string) (int, error) { if k == "error" { return 0, errors.New(k) } return len(k), nil } func doubleLength(k string) (int, error) { v, err := length(k) return 2 * v, err }
Output: Scene: default loader only 1 <nil> 0 error Scene: get loader only 1 <nil> 0 error Scene: get loader overwrites default loader 2 <nil>
type Memo ¶
type Memo[K comparable, V any] struct { // contains filtered or unexported fields }
Memo is an in-memory k-v storage, which supports concurrently get/set/delete k-v pairs. The most special place is that it can load value if not found, and can set an expiration time.
func New ¶
func New[K comparable, V any](opts ...Option[K, V]) *Memo[K, V]
New creates a memo with options.
func (*Memo[K, V]) Del ¶
func (m *Memo[K, V]) Del(k K)
Del removes the key-value pair from the memo.
type Option ¶
type Option[K comparable, V any] func(*options[K, V])
Option specifies the option when creating a new memo.
func WithClock ¶
func WithClock[K comparable, V any](clock Clock) Option[K, V]
WithClock provides a clock option when creating a new memo.
func WithExpiration ¶
func WithExpiration[K comparable, V any](expiration time.Duration) Option[K, V]
WithExpiration provides an expiration option when creating a new memo.
func WithLoader ¶
func WithLoader[K comparable, V any](loader Loader[K, V]) Option[K, V]
WithLoader provides a loader option when creating a new memo.
type SetOption ¶
type SetOption[K comparable, V any] func(*setOptions[K, V])
SetOption specifies the option when setting a value to the memo.
func SetWithExpiration ¶
func SetWithExpiration[K comparable, V any](expiration time.Duration) SetOption[K, V]
SetWithExpiration provides an expiration option when setting a value to the memo.