lru

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 5 Imported by: 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 = errors.New("lru: must provide a positive size")

ErrNoPositiveSize indicates that the size for the LRU cache must be a positive number.

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