base

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

EvictionReasons is a list of all eviction reasons.

Functions

This section is empty.

Types

type CacheMode added in v0.7.0

type CacheMode string
const (
	CacheModeMain CacheMode = "main"
	// CacheModeShared  CacheMode = "shared".
	CacheModeMissing CacheMode = "missing"
)

type EvictionCallback added in v0.2.0

type EvictionCallback[K comparable, V any] func(EvictionReason, K, V)

EvictionCallback is a function type that is called when an item is evicted from the cache. The callback receives the key and value of the evicted item. This is useful for cleanup operations, logging, or monitoring when items are removed from the cache. Common use cases include: - Cleaning up resources associated with cached values - Logging eviction events for debugging or monitoring - Updating external metrics or statistics - Triggering background operations when items are removed The callback is called synchronously during the eviction process, so it should be fast to avoid blocking cache operations. For expensive operations, consider using a goroutine.

type EvictionReason added in v0.7.0

type EvictionReason string

EvictionReason is a type that represents the reason for eviction.

const (
	EvictionReasonCapacity EvictionReason = "capacity"
	EvictionReasonTTL      EvictionReason = "ttl"
	EvictionReasonManual   EvictionReason = "manual"
	EvictionReasonStale    EvictionReason = "stale"
)

type InMemoryCache

type InMemoryCache[K comparable, V any] interface {
	// Set stores a key-value pair in the cache.
	Set(key K, value V)

	// Has checks if a key exists in the cache.
	Has(key K) bool

	// Get retrieves a value from the cache.
	// Returns the value and a boolean indicating if the key was found.
	Get(key K) (V, bool)

	// Peek retrieves a value from the cache without updating access order.
	// Returns the value and a boolean indicating if the key was found.
	Peek(key K) (V, bool)

	// Keys returns all keys currently in the cache.
	Keys() []K

	// Values returns all values currently in the cache.
	Values() []V

	// All returns all key-value pairs in the cache.
	All() map[K]V

	// Range iterates over all key-value pairs in the cache.
	// The iteration stops if the function returns false.
	Range(f func(K, V) bool)

	// Delete removes a key from the cache.
	// Returns true if the key was found and removed, false otherwise.
	Delete(key K) bool

	// Purge removes all keys and values from the cache.
	Purge()

	// SetMany stores multiple key-value pairs in the cache.
	SetMany(items map[K]V)

	// HasMany checks if multiple keys exist in the cache.
	// Returns a map where keys are the input keys and values indicate existence.
	HasMany(keys []K) map[K]bool

	// GetMany retrieves multiple values from the cache.
	// Returns a map of found key-value pairs and a slice of missing keys.
	GetMany(keys []K) (map[K]V, []K)

	// PeekMany retrieves multiple values from the cache without updating access order.
	// Returns a map of found key-value pairs and a slice of missing keys.
	PeekMany(keys []K) (map[K]V, []K)

	// DeleteMany removes multiple keys from the cache.
	// Returns a map where keys are the input keys and values indicate if the key was found and removed.
	DeleteMany(keys []K) map[K]bool

	// Capacity returns the maximum number of items the cache can hold.
	Capacity() int

	// Algorithm returns the name of the eviction algorithm used by the cache.
	Algorithm() string

	// Len returns the current number of items in the cache.
	Len() int

	// SizeBytes returns the total size of all cache entries in bytes.
	SizeBytes() int64
}

InMemoryCache represents a cache abstraction for in-memory storage. The setters and getters do not return errors because the cache is in-memory. Remote caches such as Redis, Memcached, etc. should be implemented as a chain of loaders.

Jump to

Keyboard shortcuts

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