cache

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictCallback

type EvictCallback[K comparable, V any] func(key K, value V)

EvictCallback is used to get a callback when a cache entry is evicted

type LruCache

type LruCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LruCache is a thread-safe, in-memory lru-cache that evicts the least recently used entries from memory when (if set) the entries are older than maxAge (in seconds). Use the New constructor to create one.

func New

func New[K comparable, V any](options ...Option[K, V]) *LruCache[K, V]

New creates an LruCache

func (*LruCache[K, V]) Clear

func (c *LruCache[K, V]) Clear() error

func (*LruCache[K, V]) CloneTo

func (c *LruCache[K, V]) CloneTo(n *LruCache[K, V])

CloneTo clone and overwrite elements to another LruCache

func (*LruCache[K, V]) Delete

func (c *LruCache[K, V]) Delete(key K)

Delete removes the value associated with a key.

func (*LruCache[K, V]) Exist

func (c *LruCache[K, V]) Exist(key K) bool

Exist returns if key exist in cache but not put item to the head of linked list

func (*LruCache[K, V]) Get

func (c *LruCache[K, V]) Get(key K) (V, bool)

Get returns the any representation of a cached response and a bool set to true if the key was found.

func (*LruCache[K, V]) GetOrStore

func (c *LruCache[K, V]) GetOrStore(key K, constructor func() V) (V, bool)

func (*LruCache[K, V]) GetWithExpire

func (c *LruCache[K, V]) GetWithExpire(key K) (V, time.Time, bool)

GetWithExpire returns the any representation of a cached response, a time.Time Give expected expires, and a bool set to true if the key was found. This method will NOT check the maxAge of element and will NOT update the expires.

func (*LruCache[K, V]) Set

func (c *LruCache[K, V]) Set(key K, value V)

Set stores the any representation of a response for a given key.

func (*LruCache[K, V]) SetWithExpire

func (c *LruCache[K, V]) SetWithExpire(key K, value V, expires time.Time)

SetWithExpire stores the any representation of a response for a given key and given expires. The expires time will round to second.

type Option

type Option[K comparable, V any] func(*LruCache[K, V])

Option is part of Functional Options Pattern

func WithAge

func WithAge[K comparable, V any](maxAge int64) Option[K, V]

WithAge defined element max age (second)

func WithEvict

func WithEvict[K comparable, V any](cb EvictCallback[K, V]) Option[K, V]

WithEvict set the evict callback

func WithSize

func WithSize[K comparable, V any](maxSize int) Option[K, V]

WithSize defined max length of LruCache

func WithStale

func WithStale[K comparable, V any](stale bool) Option[K, V]

WithStale decide whether Stale return is enabled. If this feature is enabled, element will not get Evicted according to `WithAge`.

func WithUpdateAgeOnGet

func WithUpdateAgeOnGet[K comparable, V any]() Option[K, V]

WithUpdateAgeOnGet update expires when Get element

Jump to

Keyboard shortcuts

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