cache

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: LGPL-3.0 Imports: 6 Imported by: 1

README

cache Go Report CardGo ReferenceGo Documentation

Thread-safe implementation of different cache algorithms in golang. Blazing fast Get()s, quite slow Set()s.

issues | lists

go get git.sr.ht/~xn/cache/v2
// example: LRU

lru := cache.NewLRU[int](1000)
lru.Set("key", 10)

If you need only specific cache algorithm, you can import it without additional dependencies:

go get git.sr.ht/~xn/cache/v2/lfu
lfuCache := lfu.New[int](1000)
lfuCache.Set("key", 10)

status

implemented
tests
total:						(statements)	100.0%
benchmarks
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/v2/lfu
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	6875901	      205.2 ns/op	     24 B/op	      1 allocs/op
BenchmarkSetX2-8    	  38306	   122613 ns/op	     27 B/op	      1 allocs/op
BenchmarkGet-8      	8738271	      171.2 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	14548699	      122.9 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	9629253	      165.6 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  10000	   194397 ns/op	 458826 B/op	      3 allocs/op
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/v2/lru
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	5875112	      210.3 ns/op	     25 B/op	      1 allocs/op
BenchmarkSetX2-8    	  39282	   148461 ns/op	     26 B/op	      1 allocs/op
BenchmarkGet-8      	5668399	      229.2 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	14897650	       95.25 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	9606914	      147.4 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  10000	   174681 ns/op	 458827 B/op	      3 allocs/op
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/v2/tlru
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	5374263	      241.1 ns/op	     32 B/op	      1 allocs/op
BenchmarkSetX2-8    	  37250	   143659 ns/op	     34 B/op	      1 allocs/op
BenchmarkGet-8      	5046880	      267.7 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	4878238	      273.1 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	9387120	      145.5 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  10000	   184600 ns/op	 458826 B/op	      3 allocs/op
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/v2/kv
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	3892966	      386.1 ns/op	    167 B/op	      0 allocs/op
BenchmarkGet-8      	7877655	      170.0 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	11948602	      123.3 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	8090110	      165.6 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	22674961	       51.58 ns/op	     48 B/op	      1 allocs/op
PASS
ok  	git.sr.ht/~xn/cache/v2/kv	29.590s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[V any] interface {
	// Get an item from cache
	Get(key string) V
	// Set an item to cache
	Set(key string, value V)
	// Has an item in cache
	Has(key string) bool
	// Remove an item from cache
	Remove(key string)
	// Purge all items from cache
	Purge()
}

Cache interface, used by any cache implementation

func NewKV added in v2.1.0

func NewKV[V any]() Cache[V]

NewKV creates a Key Value store

func NewLFU

func NewLFU[V any](size int) Cache[V]

NewLFU creates new Least Frequently Used cache

func NewLRU

func NewLRU[V any](size int) Cache[V]

NewLRU creates new Least Recently Used cache

func NewNull

func NewNull[V any]() Cache[V]

NewNull creates an empty cache client, usable for testing

func NewTLRU

func NewTLRU[V any](size int, ttl time.Duration, stale bool) Cache[V]

NewTLRU cerates new Time aware Least Recently Used cache Arguments: size - max amount if items in cache ttl - cached item expiration duration stale - if true, stale cached item will be returned instead of nil, but only once

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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