cache

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache implements LoadingCache.

Support LRC TTL-based eviction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoadingCache

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

LoadingCache provides expirable loading cache with LRC eviction.

func NewLoadingCache

func NewLoadingCache(options ...Option) (*LoadingCache, error)

NewLoadingCache returns a new expirable LRC cache, activates purge with purgeEvery (0 to never purge). Default MaxKeys is unlimited (0).

func (*LoadingCache) Close

func (c *LoadingCache) Close()

Close cleans the cache and destroys running goroutines

func (*LoadingCache) DeleteExpired

func (c *LoadingCache) DeleteExpired()

DeleteExpired clears cache of expired items

func (*LoadingCache) Get

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

Get returns the key value

func (*LoadingCache) Invalidate

func (c *LoadingCache) Invalidate(key string)

Invalidate key (item) from the cache

func (*LoadingCache) InvalidateFn

func (c *LoadingCache) InvalidateFn(fn func(key string) bool)

InvalidateFn deletes multiple keys if predicate is true

func (*LoadingCache) ItemCount

func (c *LoadingCache) ItemCount() int

ItemCount return count of items in cache

func (*LoadingCache) Keys

func (c *LoadingCache) Keys() []string

Keys return slice of current keys in the cache

func (*LoadingCache) Peek

func (c *LoadingCache) Peek(key string) (interface{}, bool)

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

func (*LoadingCache) Purge

func (c *LoadingCache) Purge()

Purge clears the cache completely.

func (*LoadingCache) Set

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

Set key

type Option

type Option func(lc *LoadingCache) error

Option func type

func MaxKeys

func MaxKeys(max int) Option

MaxKeys functional option defines how many keys to keep. By default it is 0, which means unlimited. If any non-zero MaxKeys set, default PurgeEvery will be set to 5 minutes

func OnEvicted

func OnEvicted(fn func(key string, value interface{})) Option

OnEvicted called automatically for expired and manually deleted entries

func PurgeEvery

func PurgeEvery(interval time.Duration) Option

PurgeEvery functional option defines purge interval by default it is 0, i.e. never. If MaxKeys set to any non-zero this default will be 5minutes

func TTL

func TTL(ttl time.Duration) Option

TTL functional option defines TTL for all cache entries. By default it is set to 10 years, sane option for expirable cache might be 5 minutes.

Jump to

Keyboard shortcuts

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