cache

package
v1.2.96 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache Cache item implementation Based on https://github.com/ReneKroon/ttlcache

Index

Constants

View Source
const (
	// ItemNotExpire Will avoid the cachedItem being expired by TTL, but can still be exired by callback etc.
	ItemNotExpire time.Duration = -1

	// ItemExpireWithGlobalTTL will use the global TTL when set.
	ItemExpireWithGlobalTTL time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a synchronized map of items that can auto-expire once stale

func NewTtlCache

func NewTtlCache[K comparable, T any]() *Cache[K, T]

NewTtlCache is a helper to create instance of the Cache struct

func (*Cache[K, T]) Close

func (cache *Cache[K, T]) Close()

Close calls Purge, and then stops the goroutine that does ttl checking, for a clean shutdown. The cache is no longer cleaning up after the first call to Close, repeated calls are safe though.

func (*Cache[K, T]) Count

func (cache *Cache[K, T]) Count() int

Count returns the number of items in the cache

func (*Cache[K, T]) Delete

func (cache *Cache[K, T]) Delete(key K)

Delete deletes the key value.

func (*Cache[K, T]) Get

func (cache *Cache[K, T]) Get(key K) (T, bool)

Get is a thread-safe way to lookup items Every lookup, also touches the cachedItem, hence extending it's life

func (*Cache[K, T]) Load

func (cache *Cache[K, T]) Load(key K) (T, bool)

Load returns key value.

func (*Cache[K, T]) Purge

func (cache *Cache[K, T]) Purge()

Purge will remove all entries

func (*Cache[K, T]) Range

func (cache *Cache[K, T]) Range(cb func(k K, v T) bool)

Range iterates over all items in the cache

func (*Cache[K, T]) Remove

func (cache *Cache[K, T]) Remove(key K) bool

func (*Cache[K, T]) Set

func (cache *Cache[K, T]) Set(key K, data T)

Set is a thread-safe way to add new items to the map

func (*Cache[K, T]) SetCheckExpirationCallback

func (cache *Cache[K, T]) SetCheckExpirationCallback(callback checkExpireCallback[K, T])

SetCheckExpirationCallback sets a callback that will be called when an cachedItem is about to expire in order to allow external code to decide whether the cachedItem expires or remains for another TTL cycle

func (*Cache[K, T]) SetExpirationCallback

func (cache *Cache[K, T]) SetExpirationCallback(callback expireCallback[K, T])

SetExpirationCallback sets a callback that will be called when an cachedItem expires

func (*Cache[K, T]) SetNewItemCallback

func (cache *Cache[K, T]) SetNewItemCallback(callback expireCallback[K, T])

SetNewItemCallback sets a callback that will be called when a new cachedItem is added to the cache

func (*Cache[K, T]) SetTTL

func (cache *Cache[K, T]) SetTTL(ttl time.Duration)

func (*Cache[K, T]) SetWithTTL

func (cache *Cache[K, T]) SetWithTTL(key K, data T, ttl time.Duration)

SetWithTTL is a thread-safe way to add new items to the map with individual ttl

func (*Cache[K, T]) SkipTtlExtensionOnHit

func (cache *Cache[K, T]) SkipTtlExtensionOnHit(value bool)

SkipTtlExtensionOnHit allows the user to change the cache behaviour. When this flag is set to true it will no longer extend TTL of items when they are retrieved using Get, or when their expiration condition is evaluated using SetCheckExpirationCallback.

func (*Cache[K, T]) Store

func (cache *Cache[K, T]) Store(key K, value T)

Store sets the key value.

func (*Cache[K, T]) StoreWithTTL

func (cache *Cache[K, T]) StoreWithTTL(key K, value T, ttl time.Duration)

StoreWithTTL sets the key value with TTL overrides the default.

Jump to

Keyboard shortcuts

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