memo

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2021 License: BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func GetWithExpiration(expiration time.Duration) GetOption

GetWithExpiration provides an expiration option when getting a value from the memo.

func GetWithLoader

func GetWithLoader(loader Loader) GetOption

GetWithLoader provides a loader option when getting a value from the memo.

type Key

type Key = interface{}

Key specifies the key type of memo.

type Loader

type Loader func(Key) (Value, error)

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.

func New

func New(opts ...Option) *Memo

New creates a memo with options.

func (*Memo) Del

func (m *Memo) Del(k Key)

Del removes the key-value pair from the memo.

func (*Memo) Get

func (m *Memo) Get(k Key, opts ...GetOption) (Value, error)

Get returns the associated value of the key. If the value is not found(or expired) but a loader is provided, the loader will be invoked to get a new value. If a new value is loaded and an expiration option is provided, the expiration option will act on the new value.

func (*Memo) Set

func (m *Memo) Set(k Key, v Value, opts ...SetOption)

Set inserts a key-value pair into the memo, if the key already exists, update the associated value directly. If an expiration is provided, it will act on the pair.

type Option

type Option func(*options)

Option specifies the option when creating a new memo.

func WithClock

func WithClock(clock Clock) Option

WithClock provides a clock option when creating a new memo.

func WithExpiration

func WithExpiration(expiration time.Duration) Option

WithExpiration provides an expiration option when creating a new memo.

func WithLoader

func WithLoader(loader Loader) Option

WithLoader provides a loader option when creating a new memo.

type RealClock

type RealClock struct{}

A RealClock can provide the real current time.

func NewRealClock

func NewRealClock() RealClock

NewRealClock creates a real clock.

func (RealClock) Now

func (rc RealClock) Now() int64

Now returns the real current time in nanoseconds.

type SetOption

type SetOption func(*setOptions)

SetOption specifies the option when setting a value to the memo.

func SetWithExpiration

func SetWithExpiration(expiration time.Duration) SetOption

SetWithExpiration provides an expiration option when setting a value to the memo.

type Value

type Value = interface{}

Value specifies the value type of memo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL