cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 4 Imported by: 2

README

cache

Package cache provides a in-memory cache.

Documentation

Overview

Package cache provides a in-memory cache.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSize is returned by a constructor of `Cache` when the max size is not proper.
	ErrInvalidSize = errors.New("InvalidSize")
	// ErrNoSource is returned by a constructor of `Cache` when the source function is nil.
	ErrNoSource = errors.New("NoSource")
)

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] interface {
	Get(key K) (V, error)
}

Cache is a in-memory cache.

Example
package main

import (
	"fmt"

	"github.com/berquerant/cache"
)

func main() {
	c, err := cache.NewLRU(3, func(x int) (int, error) {
		fmt.Printf("src: %d\n", x)
		return x * x, nil
	})
	if err != nil {
		panic(err)
	}
	get := func(x int) {
		y, _ := c.Get(x)
		fmt.Println(y)
	}

	for _, x := range []int{
		1, 1, 2, 3, 4, 3, 1,
	} {
		get(x)
	}

	fmt.Println(c.Hit(), c.Miss(), c.Size())
}
Output:

src: 1
1
1
src: 2
4
src: 3
9
src: 4
16
9
src: 1
1
2 5 3

type FIFO

type FIFO[K comparable, V any] struct {
	sync.RWMutex
	Stat
	// contains filtered or unexported fields
}

FIFO implements a FIFO cache.

func NewFIFO

func NewFIFO[K comparable, V any](size int, source Source[K, V]) (*FIFO[K, V], error)

NewFIFO returns a new FIFO cache. It can save `size` values at most. If a cache miss occurs, this tries to get a value from `source`.

func (*FIFO[K, V]) Get

func (f *FIFO[K, V]) Get(key K) (V, error)

func (*FIFO[K, V]) String

func (f *FIFO[K, V]) String() string

type LRU

type LRU[K comparable, V any] struct {
	sync.RWMutex
	Stat
	// contains filtered or unexported fields
}

LRU implements a Least-Recentry-Uses cache.

func NewLRU

func NewLRU[K comparable, V any](size int, source Source[K, V]) (*LRU[K, V], error)

NewLRU returns a new LRU cache. It can save `size` values at most. If a cache miss occurs, this tries to get a value from `source`.

func (*LRU[K, V]) Get

func (lru *LRU[K, V]) Get(key K) (V, error)

func (*LRU[K, V]) String

func (lru *LRU[K, V]) String() string

type Single

type Single[K comparable, V any] struct {
	sync.RWMutex
	Stat
	// contains filtered or unexported fields
}

Single is a cache that saves a value at most.

func NewSingle

func NewSingle[K comparable, V any](source Source[K, V]) (*Single[K, V], error)

NewSingle returns a new Single cache. If a cache miss occurs, this tries to get a value from `source`.

func (*Single[K, V]) Get

func (s *Single[K, V]) Get(key K) (V, error)

func (*Single[K, V]) String

func (s *Single[K, V]) String() string

type Source

type Source[K comparable, V any] func(K) (V, error)

Source is a data source of a cache.

type Stat

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

A simple cache statistics report.

func (*Stat) Hit

func (s *Stat) Hit() int

func (*Stat) Miss

func (s *Stat) Miss() int

func (*Stat) Size

func (s *Stat) Size() int

Jump to

Keyboard shortcuts

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