cache

package
v1.1.4 Latest Latest
Warning

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

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

Documentation

Overview

* TTLCache - an in-memory cache with expiration * TTLCache is a simple key/value cache in golang with the following functions: * * 1. Thread-safe * 2. Individual expiring time or global expiring time, you can choose * 3. Auto-Extending expiration on Get -or- DNS style TTL, see SkipTtlExtensionOnHit(bool) * 4. Fast and memory efficient * 5. Can trigger callback on key expiration * 6. Cleanup resources by calling Close() at end of lifecycle. * * Based on https://github.com/ReneKroon/ttlcache

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

* Priority queue 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 struct {
	// contains filtered or unexported fields
}

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

func NewTtlCache

func NewTtlCache() *Cache

NewCache is a helper to create instance of the Cache struct

func (*Cache) Close

func (cache *Cache) 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) Count

func (cache *Cache) Count() int

Count returns the number of items in the cache

func (*Cache) Delete

func (cache *Cache) Delete(key any)

Delete deletes the key value.

func (*Cache) Get

func (cache *Cache) Get(key string) (any, bool)

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

func (*Cache) Load

func (cache *Cache) Load(key any) (any, bool)

Load returns key value.

func (*Cache) Purge

func (cache *Cache) Purge()

Purge will remove all entries

func (*Cache) Range

func (cache *Cache) Range(cb func(k, v any) bool)

Iterate over all items in the cache

func (*Cache) Remove

func (cache *Cache) Remove(key string) bool

func (*Cache) Set

func (cache *Cache) Set(key string, data any)

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

func (*Cache) SetCheckExpirationCallback

func (cache *Cache) SetCheckExpirationCallback(callback checkExpireCallback)

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) SetExpirationCallback

func (cache *Cache) SetExpirationCallback(callback expireCallback)

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

func (*Cache) SetNewItemCallback

func (cache *Cache) SetNewItemCallback(callback expireCallback)

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

func (*Cache) SetTTL

func (cache *Cache) SetTTL(ttl time.Duration)

func (*Cache) SetWithTTL

func (cache *Cache) SetWithTTL(key string, data any, ttl time.Duration)

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

func (*Cache) SkipTtlExtensionOnHit

func (cache *Cache) 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) Store

func (cache *Cache) Store(key any, value any)

Store sets the key value.

func (*Cache) StoreWithTTL

func (cache *Cache) StoreWithTTL(key any, value any, 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