golru

package module
v0.0.0-...-a91ca5a Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2020 License: MIT Imports: 3 Imported by: 0

README

Golru [WIP]

Golru is a simple LRU cache implementation suitable for parallel access.

It's based (loosely) on some of the ristretto ideas, however much less sophisticated.

It uses map sharding and batching to improve availability and reduce number of locks.

Let's get to examples:

	c := NewCache(cacheSize, nil)
	for i := 0; i < 100; i++ {
		k := strconv.Itoa(i)
		ok := c.Add(k, k)
	}
	for i := 0; i < 50; i++ {
		k := strconv.Itoa(i)
		val, ok := c.Get(k)
	}
	for i := 50; i < 100; i++ {
		k := strconv.Itoa(i)
		ok := c.Remove(k)
	}

for more sophisticated examples with concurrent access see test files.

To install golru run

go get -u github.com/aleert/golru

You can also run tests with

go test . -race

TODO

Add some benchmarks

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MemHashString

func MemHashString(str string) uint64

MemHashString provides fast hashing for strings

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a thread-safe LRU cache that utilizes map sharding to provide parallel acess to elements and batching to reduce cache lock time it means that it has eventual element priority update and removal rather than immediate ones

func NewCache

func NewCache(cp uint, conf *CacheConfig) *Cache

New cache return Cache with specified config default thresholds are cap*0.02 + 2 for getThresh and cap*0.01 + 2 fof delThresh

func (*Cache) Add

func (c *Cache) Add(k, v string) bool

Add returns true on success, false if such key already in cache

func (*Cache) Get

func (c *Cache) Get(k string) (val string, ok bool)

Get returns cache value, setting ok to false if no value present in cache

func (*Cache) Len

func (c *Cache) Len() int

func (*Cache) Remove

func (c *Cache) Remove(k string) bool

Remove deletes element with key k from cache and returns ok, false if k not in cache Remove blocks cache until value is deleted

type CacheConfig

type CacheConfig struct {
	GetThresh uint
	DelThresh uint
}

CacheConfig controls when cache metadata updates values update their priorities when more than GetThresh values were accessed and remove when size of cache exceeds its capacity + DelThresh to implement immediate updates set both thresholds to 0

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is a shard value with additional metadata

Jump to

Keyboard shortcuts

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