cache

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 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

issues | lists

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

lru := cache.NewLRU(1000)
lru.Set(1, 10)

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

go get git.sr.ht/~xn/cache/lfu
lfuCache := lfu.New(1000)
lfuCache.Set(1, 10)

status

implemented
tests
total:						(statements)	96.2%
benchmarks
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/lfu
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	4637701	      222.0 ns/op	     47 B/op	      2 allocs/op
BenchmarkSetX2-8    	  36528	     121528 ns/op	     50 B/op	      3 allocs/op
BenchmarkGet-8      	7890040	      157.2 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	8764112	      154.3 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	8780908	      146.5 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  14826	     199172 ns/op	 909389 B/op	      3 allocs/op
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/lru
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	4089240	      300.2 ns/op	     47 B/op	      2 allocs/op
BenchmarkSetX2-8    	  37603	     120438 ns/op	     50 B/op	      3 allocs/op
BenchmarkGet-8      	5671987	      248.3 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8      	8222047	      158.3 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	8205411	      142.4 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  10000	     152320 ns/op	 458828 B/op	      3 allocs/op
goos: linux
goarch: amd64
pkg: git.sr.ht/~xn/cache/tlru
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
BenchmarkSet-8      	4036375	      315.4 ns/op	     55 B/op	      2 allocs/op
BenchmarkSetX2-8    	  36310	     117559 ns/op	     57 B/op	      3 allocs/op
BenchmarkGet-8      	4849862	      259.5 ns/op	      0 B/op	      0 allocs/op
BenchmarkHas-8         10223767	      155.3 ns/op	      0 B/op	      0 allocs/op
BenchmarkRemove-8   	9375744	      141.9 ns/op	      0 B/op	      0 allocs/op
BenchmarkPurge-8    	  15564	     173270 ns/op	 909389 B/op	      3 allocs/op
v2 todo

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache interface, used by any cache implementation

func NewLFU

func NewLFU(size int) Cache

NewLFU creates new Least Frequently Used cache

func NewLRU

func NewLRU(size int) Cache

NewLRU creates new Least Recently Used cache

func NewMemcached added in v1.2.0

func NewMemcached(ttl int32, servers ...string) Cache

NewMemcached creates new Memcached client

func NewNull added in v1.1.0

func NewNull() Cache

NewNull creates an empty cache client, usable for testing

func NewTLRU

func NewTLRU(size int, ttl time.Duration, stale bool) Cache

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
Package memcached is a wrapper of github.com/bradfitz/gomemcache with Cache interface
Package memcached is a wrapper of github.com/bradfitz/gomemcache with Cache interface

Jump to

Keyboard shortcuts

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