timedcache

package
v0.13.0-pre.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TimedCache

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

TimedCache defines a new cache, where entries are removed after exceeding their ttl. The entry is not guaranteed to live this long (i.e. if it gets evicted when the cache fills up). Conversely, the entry also isn't guaranteed to expire at exactly the ttl time. The expiration mechanism is 'lazy', and will only remove expired objects at next access.

func New

func New(size int, ttl int) (*TimedCache, error)

New creates a new cache with a given size and ttl. TTL defines the time in seconds an entry shall live, before being expired.

func NewWithEvict

func NewWithEvict(size int, ttl int, onEvicted func(key, value interface{})) (*TimedCache, error)

NewWithEvict constructs a fixed size cache with the given ttl & eviction callback.

func (*TimedCache) Add

func (tc *TimedCache) Add(key, value interface{}) (evicted bool)

Add adds a value to the cache. Returns true if an eviction occurred.

func (*TimedCache) Contains

func (tc *TimedCache) Contains(key interface{}) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*TimedCache) ContainsOrAdd

func (tc *TimedCache) ContainsOrAdd(key, value interface{}) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness, ttl, or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*TimedCache) Get

func (tc *TimedCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache, removing it if it has expired.

func (*TimedCache) GetOldest

func (tc *TimedCache) GetOldest() (key, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*TimedCache) Keys

func (tc *TimedCache) Keys() []interface{}

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

func (*TimedCache) Len

func (tc *TimedCache) Len() int

Len returns the number of items in the cache.

func (*TimedCache) Peek

func (tc *TimedCache) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness or ttl of the key.

func (*TimedCache) PeekOrAdd

func (tc *TimedCache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool)

PeekOrAdd checks if a key is in the cache without updating the recent-ness, ttl, or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*TimedCache) Purge

func (tc *TimedCache) Purge()

Purge is used to completely clear the cache.

func (*TimedCache) Remove

func (tc *TimedCache) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache.

func (*TimedCache) RemoveOldest

func (tc *TimedCache) RemoveOldest() (key, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*TimedCache) Resize

func (tc *TimedCache) Resize(size int) (evicted int)

Resize changes the cache size.

func (*TimedCache) Ttl

func (tc *TimedCache) Ttl() int64

Ttl returns the number of seconds each item is allowed to live (except if evicted to free up space)

Jump to

Keyboard shortcuts

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