cache

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 1 Imported by: 8

README

cache

import "github.com/zyedidia/generic/cache"

Package cache provides an implementation of a key-value store with a maximum size. Once the maximum size is reached, the cache uses a least-recently-used policy to evict old entries. The cache is implemented as a combined hashmap and linked list. This ensures all operations are constant-time.

Example

package main

import (
	"fmt"

	"github.com/zyedidia/generic/cache"
)

func main() {
	c := cache.New[int, int](2)

	c.Put(42, 42)
	c.Put(10, 10)
	c.Get(42)
	c.Put(0, 0) // evicts 10

	c.Each(func(key, val int) {
		fmt.Println("each", key)
	})

	c.Resize(3)
	fmt.Println("size", c.Size())
	fmt.Println("capacity", c.Capacity())

	c.SetEvictCallback(func(key, val int) {
		fmt.Println("evict", key)
	})
	c.Put(1, 1)
	c.Put(2, 2) // evicts 42
	c.Remove(3) // no effect
	c.Resize(1) // evicts 0 and 1

	c.Each(func(key, val int) {
		fmt.Println("each", key)
	})

}
Output
each 0
each 42
size 2
capacity 3
evict 42
evict 0
evict 1
each 2

Index

type Cache

A Cache is an LRU cache for keys and values. Each entry is put into the table with an associated key used for looking up the entry. The cache has a maximum size, and uses a least-recently-used eviction policy when there is not space for a new entry.

type Cache[K comparable, V any] struct {
    // contains filtered or unexported fields
}
func New
func New[K comparable, V any](capacity int) *Cache[K, V]

New returns a new Cache with the given capacity.

func (*Cache[K, V]) Capacity
func (t *Cache[K, V]) Capacity() int

Capacity returns the maximum capacity of the cache.

func (*Cache[K, V]) Each
func (t *Cache[K, V]) Each(fn func(key K, val V))

Each calls 'fn' on every value in the cache, from most recently used to least recently used.

func (*Cache[K, V]) Get
func (t *Cache[K, V]) Get(k K) (V, bool)

Get returns the entry associated with a given key, and a boolean indicating whether the key exists in the table.

func (*Cache[K, V]) Put
func (t *Cache[K, V]) Put(k K, e V)

Put adds a new key-entry pair to the table.

func (*Cache[K, V]) Remove
func (t *Cache[K, V]) Remove(k K)

Remove causes the entry associated with the given key to be immediately evicted from the cache.

func (*Cache[K, V]) Resize
func (t *Cache[K, V]) Resize(capacity int)

Resize changes the maximum capacity for this cache to 'capacity'.

func (*Cache[K, V]) SetEvictCallback
func (t *Cache[K, V]) SetEvictCallback(fn func(key K, val V))

SetEvictCallback sets a callback to be invoked before an entry is evicted. This replaces any prior callback set by this method.

func (*Cache[K, V]) Size
func (t *Cache[K, V]) Size() int

Size returns the number of active elements in the cache.

type KV

type KV[K comparable, V any] struct {
    Key K
    Val V
}

Generated by gomarkdoc

Documentation

Overview

Package cache provides an implementation of a key-value store with a maximum size. Once the maximum size is reached, the cache uses a least-recently-used policy to evict old entries. The cache is implemented as a combined hashmap and linked list. This ensures all operations are constant-time.

Example
package main

import (
	"fmt"

	"github.com/zyedidia/generic/cache"
)

func main() {
	c := cache.New[int, int](2)

	c.Put(42, 42)
	c.Put(10, 10)
	c.Get(42)
	c.Put(0, 0) // evicts 10

	c.Each(func(key, val int) {
		fmt.Println("each", key)
	})

	c.Resize(3)
	fmt.Println("size", c.Size())
	fmt.Println("capacity", c.Capacity())

	c.SetEvictCallback(func(key, val int) {
		fmt.Println("evict", key)
	})
	c.Put(1, 1)
	c.Put(2, 2) // evicts 42
	c.Remove(3) // no effect
	c.Resize(1) // evicts 0 and 1

	c.Each(func(key, val int) {
		fmt.Println("each", key)
	})

}
Output:

each 0
each 42
size 2
capacity 3
evict 42
evict 0
evict 1
each 2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

A Cache is an LRU cache for keys and values. Each entry is put into the table with an associated key used for looking up the entry. The cache has a maximum size, and uses a least-recently-used eviction policy when there is not space for a new entry.

func New

func New[K comparable, V any](capacity int) *Cache[K, V]

New returns a new Cache with the given capacity.

func (*Cache[K, V]) Capacity

func (t *Cache[K, V]) Capacity() int

Capacity returns the maximum capacity of the cache.

func (*Cache[K, V]) Each

func (t *Cache[K, V]) Each(fn func(key K, val V))

Each calls 'fn' on every value in the cache, from most recently used to least recently used.

func (*Cache[K, V]) Get

func (t *Cache[K, V]) Get(k K) (V, bool)

Get returns the entry associated with a given key, and a boolean indicating whether the key exists in the table.

func (*Cache[K, V]) Put

func (t *Cache[K, V]) Put(k K, e V)

Put adds a new key-entry pair to the table.

func (*Cache[K, V]) Remove

func (t *Cache[K, V]) Remove(k K)

Remove causes the entry associated with the given key to be immediately evicted from the cache.

func (*Cache[K, V]) Resize

func (t *Cache[K, V]) Resize(capacity int)

Resize changes the maximum capacity for this cache to 'capacity'.

func (*Cache[K, V]) SetEvictCallback added in v1.0.0

func (t *Cache[K, V]) SetEvictCallback(fn func(key K, val V))

SetEvictCallback sets a callback to be invoked before an entry is evicted. This replaces any prior callback set by this method.

func (*Cache[K, V]) Size

func (t *Cache[K, V]) Size() int

Size returns the number of active elements in the cache.

type KV

type KV[K comparable, V any] struct {
	Key K
	Val V
}

Jump to

Keyboard shortcuts

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