slru

package module
v0.1.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: 1 Imported by: 1

README

slru

Go Reference CI Coverage Go Report Card

Thread safe GoLang S(2)LRU cache.

Example

import (
	"fmt"

	slru "github.com/floatdrop/slru"
)

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

	cache.Set("Hello", 5)

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

Benchmarks

floatdrop/slru:
	BenchmarkSLRU_Rand-8   	 5600960	       206.9 ns/op	      44 B/op	       3 allocs/op
	BenchmarkSLRU_Freq-8   	 5927858	       201.0 ns/op	      43 B/op	       3 allocs/op

Documentation

Index

Examples

Constants

View Source
const (
	DefaultProbationRatio = 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 SLRU

type SLRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"

	slru "github.com/floatdrop/slru"
)

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

	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) *SLRU[K, V]

func NewParams

func NewParams[K comparable, V any](probationSize int, protectedSize int) *SLRU[K, V]

func (*SLRU[K, V]) Get

func (S *SLRU[K, V]) Get(key K) *V

func (*SLRU[K, V]) Len

func (S *SLRU[K, V]) Len() int

func (*SLRU[K, V]) Peek

func (S *SLRU[K, V]) Peek(key K) *V

func (*SLRU[K, V]) Remove

func (S *SLRU[K, V]) Remove(key K) *V

func (*SLRU[K, V]) Set

func (S *SLRU[K, V]) Set(key K, value V) *Evicted[K, V]

func (*SLRU[K, V]) Victim

func (S *SLRU[K, V]) Victim(key K) *K

Jump to

Keyboard shortcuts

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