memo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

README

Memo License Build Status

How to use

Use as a simple map
var cache = memo.NewMemo()

func main() {
	cache.Set("x", 1)
	fmt.Println(cache.Get("x"))
}
Concurrently safe
var cache = memo.NewMemo()

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 1000000; j++ {
				cache.Set("x", j)
			}
		}()
	}
	wg.Wait()
	fmt.Println(cache.Get("x"))
}
Support expiration
var cache = memo.NewMemo()

func main() {
	cache.Set("x", 1, memo.SetWithExpiration(time.Second))
	fmt.Println(cache.Get("x"))
	time.Sleep(time.Second)
	fmt.Println(cache.Get("x"))
}
Support loader
var cache = memo.NewMemo()

func main() {
	fmt.Println(cache.Get("x"))
	loader := func(k memo.Key) (memo.Value, error) {
		return len(k.(string)), nil
	}
	fmt.Println(cache.Get("x", memo.GetWithLoader(loader)))
}
Load exactly once per key
var cache = memo.NewMemo()

func main() {
	fmt.Println(cache.Get("x"))

	var loadCounter, loadSum int64
	loader := func(k memo.Key) (memo.Value, error) {
		time.Sleep(time.Second)
		atomic.AddInt64(&loadCounter, 1)
		return int64(1), nil
	}

	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 1000000; j++ {
				value, _ := cache.Get("x", memo.GetWithLoader(loader))
				atomic.AddInt64(&loadSum, value.(int64))
			}
		}()
	}
	wg.Wait()
	fmt.Println("counter", atomic.LoadInt64(&loadCounter))
	fmt.Println("sum", atomic.LoadInt64(&loadSum))
	fmt.Println(cache.Get("x"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound represents not found error.
	ErrNotFound = errors.New("memo: not found")
	// ErrInvalidExpiration represents invalid expiration error.
	ErrInvalidExpiration = errors.New("memo: invalid expiration")
)

Functions

This section is empty.

Types

type Clock

type Clock = clock.Clock

type Expiration added in v0.0.2

type Expiration = time.Duration

type GetOption added in v0.1.0

type GetOption func(*getOptions)

func GetWithExpiration added in v0.1.0

func GetWithExpiration(expiration Expiration) GetOption

func GetWithLoader added in v0.1.0

func GetWithLoader(loader Loader) GetOption

type Key added in v0.0.2

type Key = interface{}

type Loader

type Loader func(Key) (Value, error)

type Memo

type Memo struct {
	// contains filtered or unexported fields
}

func NewMemo

func NewMemo(opts ...Option) *Memo

func (*Memo) Del added in v0.1.0

func (m *Memo) Del(k Key)

func (*Memo) Get

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

func (*Memo) Set

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

type Option

type Option func(*options)

func WithClock

func WithClock(clock Clock) Option

func WithExpiration added in v0.0.2

func WithExpiration(expiration Expiration) Option

func WithLoader added in v0.0.2

func WithLoader(loader Loader) Option

type SetOption added in v0.1.0

type SetOption func(*setOptions)

func SetWithExpiration added in v0.1.0

func SetWithExpiration(expiration Expiration) SetOption

type Value added in v0.0.2

type Value = interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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