cache

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: MIT Imports: 5 Imported by: 1

README

go-cache

A TTL cache designed to be used as a base for your own customizations, or used straight out of the box

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Start will start the cache background eviction routine with given sweep frequency.
	// If already running or a freq <= 0 provided, this is a no-op. This will block until
	// the eviction routine has started
	Start(freq time.Duration) bool

	// Stop will stop cache background eviction routine. If not running this is a no-op. This
	// will block until the eviction routine has stopped
	Stop() bool

	// SetEvictionCallback sets the eviction callback to the provided hook
	SetEvictionCallback(hook Hook)

	// SetInvalidateCallback sets the invalidate callback to the provided hook
	SetInvalidateCallback(hook Hook)

	// SetTTL sets the cache item TTL. Update can be specified to force updates of existing items in
	// the cache, this will simply add the change in TTL to their current expiry time
	SetTTL(ttl time.Duration, update bool)

	// Get fetches the value with key from the cache, extending its TTL
	Get(key string) (interface{}, bool)

	// Put attempts to place the value at key in the cache, doing nothing if
	// a value with this key already exists. Returned bool is success state
	Put(key string, value interface{}) bool

	// Set places the value at key in the cache. This will overwrite any
	// existing value, and call the update callback so. Existing values
	// will have their TTL extended upon update
	Set(key string, value interface{})

	// Has checks the cache for a value with key, this will not update TTL
	Has(key string) bool

	// Invalidate deletes a value from the cache, calling the invalidate callback
	Invalidate(key string) bool

	// Clear empties the cache, calling the invalidate callback
	Clear()

	// Size returns the current size of the cache
	Size() int
}

Cache represents a TTL cache with customizable callbacks, it exists here to abstract away the "unsafe" methods in the case that you do not want your own implementation atop TTLCache{}.

func New

func New() Cache

New returns a new initialized Cache.

type Hook

type Hook func(key string, value interface{})

Hook defines a function hook that can be supplied as a callback.

type LookupCache

type LookupCache interface {
	Cache

	// GetBy fetches a cached value by supplied lookup identifier and key
	GetBy(lookup string, key string) (interface{}, bool)

	// HasBy checks if a value is cached under supplied lookup identifier and key
	HasBy(lookup string, key string) bool

	// InvalidateBy invalidates a value by supplied lookup identifier and key
	InvalidateBy(lookup string, key string) bool
}

LookupCache is a cache built on-top of TTLCache, providing multi-key lookups for items in the cache by means of additional lookup maps. These maps simply store additional keys => original key, with hook-ins to automatically call user supplied functions on adding an item, or on updating/deleting an item to keep the LookupMap up-to-date.

func NewLookup

func NewLookup(cfg LookupCfg) LookupCache

NewLookup returns a new initialized LookupCache.

type LookupCfg

type LookupCfg struct {
	// RegisterLookups is called on init to register lookups
	// within LookupCache's internal LookupMap
	RegisterLookups func(*LookupMap)

	// AddLookups is called on each addition to the cache, to
	// set any required additional key lookups for supplied item
	AddLookups func(*LookupMap, interface{})

	// DeleteLookups is called on each eviction/invalidation of
	// an item in the cache, to remove any unused key lookups
	DeleteLookups func(*LookupMap, interface{})
}

LookupCfg is the LookupCache configuration.

type LookupMap

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

LookupMap is a structure that provides lookups for keys to primary keys under supplied lookup identifiers. This is essentially a wrapper around map[string](map[string]string).

func (*LookupMap) Delete

func (l *LookupMap) Delete(id string, key string)

Delete removes a lookup from LookupMap with supplied identifier and key.

func (*LookupMap) Get

func (l *LookupMap) Get(id string, key string) (string, bool)

Get fetches an entry's primary key for lookup identifier and key.

func (*LookupMap) Has

func (l *LookupMap) Has(id string, key string) bool

Has checks if there exists a lookup for supplied identifier and key.

func (*LookupMap) RegisterLookup

func (l *LookupMap) RegisterLookup(id string)

RegisterLookup registers a lookup identifier in the LookupMap, note this can only be doing during the cfg.RegisterLookups() hook.

func (*LookupMap) Set

func (l *LookupMap) Set(id string, key string, origKey string)

Set adds a lookup to the LookupMap under supplied lookup identifier, linking supplied key to the supplied primary (original) key.

type TTLCache

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

TTLCache is the underlying Cache implementation, providing both the base Cache interface and access to "unsafe" methods so that you may build your customized caches ontop of this structure.

func (*TTLCache) Clear

func (c *TTLCache) Clear()

func (*TTLCache) ClearUnsafe

func (c *TTLCache) ClearUnsafe()

ClearUnsafe is mutex-unprotected logic for Cache.Clean().

func (*TTLCache) Get

func (c *TTLCache) Get(key string) (interface{}, bool)

func (*TTLCache) GetUnsafe

func (c *TTLCache) GetUnsafe(key string) (interface{}, bool)

GetUnsafe is the mutex-unprotected logic for Cache.Get().

func (*TTLCache) Has

func (c *TTLCache) Has(key string) bool

func (*TTLCache) HasUnsafe

func (c *TTLCache) HasUnsafe(key string) bool

HasUnsafe is the mutex-unprotected logic for Cache.Has().

func (*TTLCache) Init

func (c *TTLCache) Init()

Init performs Cache initialization, this MUST be called.

func (*TTLCache) Invalidate

func (c *TTLCache) Invalidate(key string) bool

func (*TTLCache) InvalidateUnsafe

func (c *TTLCache) InvalidateUnsafe(key string) bool

InvalidateUnsafe is mutex-unprotected logic for Cache.Invalidate().

func (*TTLCache) Lock

func (c *TTLCache) Lock()

Lock locks the cache mutex.

func (*TTLCache) Put

func (c *TTLCache) Put(key string, value interface{}) bool

func (*TTLCache) PutUnsafe

func (c *TTLCache) PutUnsafe(key string, value interface{}) bool

PutUnsafe is the mutex-unprotected logic for Cache.Put().

func (*TTLCache) Set

func (c *TTLCache) Set(key string, value interface{})

func (*TTLCache) SetEvictionCallback

func (c *TTLCache) SetEvictionCallback(hook Hook)

func (*TTLCache) SetInvalidateCallback

func (c *TTLCache) SetInvalidateCallback(hook Hook)

func (*TTLCache) SetTTL

func (c *TTLCache) SetTTL(ttl time.Duration, update bool)

func (*TTLCache) SetUnsafe

func (c *TTLCache) SetUnsafe(key string, value interface{})

SetUnsafe is the mutex-unprotected logic for Cache.Set(), it calls externally-set functions.

func (*TTLCache) Size

func (c *TTLCache) Size() int

func (*TTLCache) SizeUnsafe

func (c *TTLCache) SizeUnsafe() int

SizeUnsafe is mutex unprotected logic for Cache.Size().

func (*TTLCache) Start added in v1.1.0

func (c *TTLCache) Start(freq time.Duration) bool

func (*TTLCache) Stop added in v1.1.0

func (c *TTLCache) Stop() bool

func (*TTLCache) Unlock

func (c *TTLCache) Unlock()

Unlock unlocks the cache mutex.

Jump to

Keyboard shortcuts

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