memo

package
v0.0.0-...-b4c42aa Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 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[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.

func (*Memo[K, V]) Get

func (m *Memo[K, V]) Get(k K, opts ...GetOption[K, V]) (V, 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[K, V]) Set

func (m *Memo[K, V]) Set(k K, v V, opts ...SetOption[K, V])

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[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 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[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.

Jump to

Keyboard shortcuts

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