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 func(*getOptions)
GetOption specifies the option when getting a value from the memo.
func GetWithExpiration ¶
GetWithExpiration provides an expiration option when getting a value from the memo.
func GetWithLoader ¶
GetWithLoader provides a loader option when getting a value from the memo.
type Loader ¶
A Loader returns the value of the key.
Example ¶
package main import ( "fmt" "reflect" "github.com/dploop/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(233)) fmt.Println("Scene: get loader only") m = memo.New() fmt.Println(m.Get("x", memo.GetWithLoader(length))) fmt.Println(m.Get(233, 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 memo.Key) (memo.Value, error) { switch v := reflect.ValueOf(k); v.Kind() { case reflect.Array, reflect.Slice, reflect.String, reflect.Map, reflect.Chan: return v.Len(), nil default: return nil, &reflect.ValueError{Method: "reflect.Value.Len", Kind: v.Kind()} } } func doubleLength(k memo.Key) (memo.Value, error) { switch v := reflect.ValueOf(k); v.Kind() { case reflect.Array, reflect.Slice, reflect.String, reflect.Map, reflect.Chan: return 2 * v.Len(), nil default: return nil, &reflect.ValueError{Method: "reflect.Value.Len", Kind: v.Kind()} } }
Output: Scene: default loader only 1 <nil> <nil> reflect: call of reflect.Value.Len on int Value Scene: get loader only 1 <nil> <nil> reflect: call of reflect.Value.Len on int Value Scene: get loader overwrites default loader 2 <nil>
type Memo ¶
type Memo 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.
type Option ¶
type Option func(*options)
Option specifies the option when creating a new memo.
func WithExpiration ¶
WithExpiration provides an expiration option when creating a new memo.
func WithLoader ¶
WithLoader provides a loader option when creating a new memo.
type SetOption ¶
type SetOption func(*setOptions)
SetOption specifies the option when setting a value to the memo.
func SetWithExpiration ¶
SetWithExpiration provides an expiration option when setting a value to the memo.