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 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址