lru

package module
v0.0.0-...-263c856 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2018 License: Apache-2.0 Imports: 5 Imported by: 2

README

LRU

Build Status Go Report Card Taylor Swift Volkswagen License

A simple LRU Cache implementation which supports TTL of cached items and fetching of keys which do not exist or have expired. You can build upon different cache mechanism, or other caches.

Docs

You can find the documentation hosted on godoc.org.

Install

go get github.com/andersnormal/lru

Benchmarks

Run the benchmarks with task benchmark. The sample benchmarks are run on a MacBook Pro (2.7 GHz Intel Core i7).

BenchmarkCache_Rand/with_size_of_4096_and_no_ttl_set-8         	 3000000	       544 ns/op
--- BENCH: BenchmarkCache_Rand/with_size_of_4096_and_no_ttl_set-8
    simple_test.go:240: hit: 0 miss: 1 ratio: 0.000000
    simple_test.go:240: hit: 0 miss: 100 ratio: 0.000000
    simple_test.go:240: hit: 861 miss: 9139 ratio: 0.094212
    simple_test.go:240: hit: 111123 miss: 888877 ratio: 0.125015
    simple_test.go:240: hit: 332972 miss: 2667028 ratio: 0.124848
BenchmarkCache_Rand/with_size_of_8092_items_and_no_ttl_set-8   	 3000000	       580 ns/op
--- BENCH: BenchmarkCache_Rand/with_size_of_8092_items_and_no_ttl_set-8
    simple_test.go:240: hit: 0 miss: 1 ratio: 0.000000
    simple_test.go:240: hit: 0 miss: 100 ratio: 0.000000
    simple_test.go:240: hit: 1333 miss: 8667 ratio: 0.153802
    simple_test.go:240: hit: 197704 miss: 802296 ratio: 0.246423
    simple_test.go:240: hit: 593771 miss: 2406229 ratio: 0.246764
BenchmarkCache_Freq/with_size_of_4096_and_no_ttl_set-8         	 3000000	       560 ns/op
--- BENCH: BenchmarkCache_Freq/with_size_of_4096_and_no_ttl_set-8
    simple_test.go:291: hit: 1 miss: 0 ratio: +Inf
    simple_test.go:291: hit: 100 miss: 0 ratio: +Inf
    simple_test.go:291: hit: 8206 miss: 1794 ratio: 4.574136
    simple_test.go:291: hit: 236672 miss: 763328 ratio: 0.310053
    simple_test.go:291: hit: 702382 miss: 2297618 ratio: 0.305700
BenchmarkCache_Freq/with_size_of_8092_items_and_no_ttl_set-8   	 3000000	       557 ns/op
--- BENCH: BenchmarkCache_Freq/with_size_of_8092_items_and_no_ttl_set-8
    simple_test.go:291: hit: 1 miss: 0 ratio: +Inf
    simple_test.go:291: hit: 100 miss: 0 ratio: +Inf
    simple_test.go:291: hit: 8200 miss: 1800 ratio: 4.555556
    simple_test.go:291: hit: 233189 miss: 766811 ratio: 0.304102
    simple_test.go:291: hit: 708765 miss: 2291235 ratio: 0.309338
PASS
ok  	lru	19.696s

License

Apache 2.0

Documentation

Overview

Package lru is a simple implementation of an LRU cache. LRU is enhanced by allowing to set a TTL for cached items. It also supports to fetch the values for non-existing keys, by providing a function to fetch these.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoPositiveSize indicates that the size for the LRU cache
	// must be a positive number.
	ErrNoPositiveSize = errors.New("Must provide a positive size")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Adds a value to the cache, or updates an item in the cache.
	// It returns true if an item needed to be removed for storing the new item.
	Add(key interface{}, value interface{}, ttl int64) bool

	// Returns the value of the provided key, and updates status of the item
	// in the cache.
	Get(key interface{}) (value interface{}, ok bool)

	// Check if a key exsists in the cache.
	Contains(key interface{}) (ok bool)

	// Expires returns the time of expiration.
	Expires(key interface{}) (expires time.Time, ok bool)

	// Fetches a value which has expired, or does not exits and fills the cache.
	Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)

	// Removes a key from the cache.
	Remove(key interface{}) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (interface{}, interface{}, bool)

	// Returns the oldest entry from the cache.
	GetOldest() (interface{}, interface{}, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []interface{}

	// Returns the number of items in the cache.
	Len() int

	// Purge is purging the full cache.
	Purge()
}

Cache is the interface for the LRU cache

func New

func New(size int) (Cache, error)

New creates a new Cache of the given size

func NewLRU

func NewLRU(size int) (Cache, error)

NewLRU returns a new instance of the LRU cache with a certain size of elements that can be stored in time.

type Item

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

Item represents the internal presentation of a cache entry

func (*Item) Expired

func (i *Item) Expired() bool

Expired is used to check if the TTL of the item has expired

func (*Item) Expires

func (i *Item) Expires() time.Time

Expires returns the time.Time when the item expires

func (*Item) Value

func (i *Item) Value() interface{}

Value returns the value of the item

type LRUCache

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

LRUCache represents the instance of an LRU cache.

func (*LRUCache) Add

func (l *LRUCache) Add(key interface{}, value interface{}, ttl int64) bool

Add is adding a key and value with a TTL to the store. Setting the TTL to 0 signales that this key will not expire.

func (*LRUCache) Contains

func (l *LRUCache) Contains(key interface{}) (ok bool)

Contains is checking if a provided key exists in the cache

func (*LRUCache) Expires

func (l *LRUCache) Expires(key interface{}) (expires time.Time, ok bool)

Expires returns the time.Time when the provided key will expire.

func (*LRUCache) Fetch

func (l *LRUCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)

Fetch is fetching a value for key that does not exists or has expired. The fetching is done by a provided function.

func (*LRUCache) Get

func (l *LRUCache) Get(key interface{}) (value interface{}, ok bool)

Get is returning the value of a provided key.

func (*LRUCache) GetOldest

func (l *LRUCache) GetOldest() (key interface{}, value interface{}, ok bool)

GetOldest returns the oldest item of the cache.

func (*LRUCache) Keys

func (l *LRUCache) Keys() []interface{}

Keys returning the keys of the current cache.

func (*LRUCache) Len

func (l *LRUCache) Len() int

Len returns the length/number of elements that are in the cache.

func (*LRUCache) Purge

func (l *LRUCache) Purge()

Purge is purging the cache.

func (*LRUCache) Remove

func (l *LRUCache) Remove(key interface{}) (ok bool)

Remove is removing a provided key from the cache.

func (*LRUCache) RemoveOldest

func (l *LRUCache) RemoveOldest() (key interface{}, value interface{}, ok bool)

RemoveOldest removes the oldest item in the cache.

type SimpleCache

type SimpleCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

cache is a thread-safe, fixed size LRU cache with TTL.

func (*SimpleCache) Add

func (c *SimpleCache) Add(key interface{}, value interface{}, ttl int64) bool

Adds a value to the cache, or updates an item in the cache. It returns true if an item needed to be removed for storing the new item.

func (*SimpleCache) Contains

func (c *SimpleCache) Contains(key interface{}) (ok bool)

Check if a key exsists in the cache.

func (*SimpleCache) Expires

func (c *SimpleCache) Expires(key interface{}) (expires time.Time, ok bool)

Expires returns the time of expiration.

func (*SimpleCache) Fetch

func (c *SimpleCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)

Fetches a value which has expired, or does not exits and fills the cache.

func (*SimpleCache) Get

func (c *SimpleCache) Get(key interface{}) (value interface{}, ok bool)

Returns the value of the provided key, and updates status of the item in the cache.

func (*SimpleCache) GetOldest

func (c *SimpleCache) GetOldest() (interface{}, interface{}, bool)

Returns the oldest entry from the cache.

func (*SimpleCache) Keys

func (c *SimpleCache) Keys() []interface{}

Returns a slice of the keys in the cache, from oldest to newest.

func (*SimpleCache) Len

func (c *SimpleCache) Len() int

Returns the number of items in the cache.

func (*SimpleCache) Purge

func (c *SimpleCache) Purge()

Purge is purging the full cache.

func (*SimpleCache) Remove

func (c *SimpleCache) Remove(key interface{}) bool

Removes a key from the cache.

func (*SimpleCache) RemoveOldest

func (c *SimpleCache) RemoveOldest() (interface{}, interface{}, bool)

Removes the oldest entry from cache.

type Sized

type Sized interface {
	Size() int64
}

Sized is the interface to a cache size calculation.

Jump to

Keyboard shortcuts

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