tannin

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MIT Imports: 3 Imported by: 0

README

tannin

Go Reference build Coverage Go Report Card

Tannin is implementation of W-TinyLRU cache, that was implemented in Caffeine as eviction policy. Later it was adopted to Ristretto cache library.

Example

import (
	"fmt"

	tannin "github.com/floatdrop/tannin"
)

func main() {
	cache := tannin.New[string, int](256, 100)

	cache.Set("Hello", 5)

	if e := cache.Get("Hello"); e != nil {
		fmt.Println(*e)
		// Output: 5
	}
}

Benchmarks

floatdrop/tannin
	BenchmarkTannin_Rand-8   	 3957745	       295.8 ns/op	      84 B/op	       7 allocs/op
	BenchmarkTannin_Freq-8   	 4598648	       255.7 ns/op	      78 B/op	       7 allocs/op

Documentation

Index

Examples

Constants

View Source
const (
	DefaultWindowRatio = 0.1
	DefaultSLRUSplit   = 0.2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Evicted

type Evicted[K comparable, V any] struct {
	Key   K
	Value V
}

Evicted holds key/value pair that was evicted from cache.

type Tannin

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

Tannin is a thread-safe fixed size cache with windowed TinyLFU eviction policy.

Example
package main

import (
	"fmt"

	tannin "github.com/floatdrop/tannin"
)

func main() {
	cache := tannin.New[string, int](256, 100)

	cache.Set("Hello", 5)

	if e := cache.Get("Hello"); e != nil {
		fmt.Println(*e)

	}
}
Output:

5

func New

func New[K comparable, V any](size int, samples int) *Tannin[K, V]

func NewParams

func NewParams[K comparable, V any](windowSize int, mainASize int, mainBSize int, samples int) *Tannin[K, V]

func (*Tannin[K, V]) Get

func (T *Tannin[K, V]) Get(key K) *V

func (*Tannin[K, V]) Len

func (T *Tannin[K, V]) Len() int

func (*Tannin[K, V]) Peek

func (T *Tannin[K, V]) Peek(key K) *V

Peek returns value for key (if key was in cache), but does not modify its recency.

func (*Tannin[K, V]) Remove

func (T *Tannin[K, V]) Remove(key K) *V

Remove method removes entry associated with key and returns pointer to removed value (or nil if entry was not in cache).

func (*Tannin[K, V]) Set

func (T *Tannin[K, V]) Set(key K, value V) *Evicted[K, V]

Jump to

Keyboard shortcuts

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