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.
type LRU ¶
type LRU[K comparable, V any] struct { sync.RWMutex Stat // contains filtered or unexported fields }
LRU implements a Least-Recentry-Uses cache.
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.
type Source ¶
type Source[K comparable, V any] func(K) (V, error)
Source is a data source of a cache.
Click to show internal directories.
Click to hide internal directories.