cache

package
v1.2.181 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 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 is a special TTL value indicating that the item should not expire based on time.
	// However, it can still be removed from the cache through other means, such as manual deletion or callbacks.
	ItemNotExpire time.Duration = -1

	// ItemExpireWithGlobalTTL is a special TTL value indicating that the item should use the cache's global TTL setting.
	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 thread-safe, in-memory key/value store with support for time-to-live (TTL) expiration. It allows for both global and per-item TTL settings and provides callbacks for various events.

Type Parameters:

K: The type of the keys, which must be comparable.
T: The type of the values stored in the cache.

func NewTtlCache

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

NewTtlCache creates and returns a new Cache instance.

func (*Cache[K, T]) Close

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

Close gracefully shuts down the cache, stopping the expiration processing goroutine and clearing all items. It is safe to call Close multiple times.

func (*Cache[K, T]) Count

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

Count returns the number of items currently in the cache.

func (*Cache[K, T]) Delete

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

Delete is an alias for Remove.

func (*Cache[K, T]) Get

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

Get retrieves an item from the cache. It also extends the item's TTL unless disabled.

func (*Cache[K, T]) Load

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

Load is an alias for Get.

func (*Cache[K, T]) Purge

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

Purge removes all items from the cache.

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 and applies a callback.

func (*Cache[K, T]) Remove

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

Remove deletes an item from the cache.

func (*Cache[K, T]) Set

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

Set adds or updates an item in the cache with the global TTL.

func (*Cache[K, T]) SetCheckExpirationCallback

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

SetCheckExpirationCallback sets a callback that allows for external validation before an item expires.

func (*Cache[K, T]) SetExpirationCallback

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

SetExpirationCallback sets the callback function to be executed when an item expires.

func (*Cache[K, T]) SetNewItemCallback

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

SetNewItemCallback sets the callback function to be executed when a new item is added to the cache.

func (*Cache[K, T]) SetTTL

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

SetTTL sets the global TTL for the cache.

func (*Cache[K, T]) SetWithTTL

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

SetWithTTL adds or updates an item in the cache with a specific TTL.

func (*Cache[K, T]) SetWithTTLOld added in v1.2.181

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

SetWithTTLOld adds or updates an item in the cache with a specific TTL.

func (*Cache[K, T]) SkipTtlExtensionOnHit

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

SkipTtlExtensionOnHit configures whether the TTL of an item is extended on a cache hit.

func (*Cache[K, T]) Store

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

Store is an alias for Set.

func (*Cache[K, T]) StoreWithTTL

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

StoreWithTTL is an alias for SetWithTTL.

Jump to

Keyboard shortcuts

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