safe

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: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSafeInMemoryCache

func NewSafeInMemoryCache[K comparable, V any](cache base.InMemoryCache[K, V]) base.InMemoryCache[K, V]

NewSafeInMemoryCache creates a thread-safe wrapper around an existing cache implementation. This function wraps any cache that implements the InMemoryCache interface with read-write mutex protection to ensure safe concurrent access from multiple goroutines.

Types

type SafeInMemoryCache

type SafeInMemoryCache[K comparable, V any] struct {
	base.InMemoryCache[K, V] // Embedded cache implementation
	sync.RWMutex             // Read-write mutex for thread safety
}

SafeInMemoryCache is a thread-safe wrapper around any cache implementation. It uses a read-write mutex to protect all cache operations, allowing multiple concurrent readers but only one writer at a time.

func (*SafeInMemoryCache[K, V]) Algorithm added in v0.7.0

func (c *SafeInMemoryCache[K, V]) Algorithm() string

Algorithm returns the name of the eviction algorithm used by the cache. This is a read-only operation that doesn't require locking as algorithm is immutable.

func (*SafeInMemoryCache[K, V]) All added in v0.9.0

func (c *SafeInMemoryCache[K, V]) All() map[K]V

All returns all key-value pairs in the cache.

func (*SafeInMemoryCache[K, V]) Capacity added in v0.7.0

func (c *SafeInMemoryCache[K, V]) Capacity() int

Capacity returns the maximum number of items the cache can hold. This is a read-only operation that doesn't require locking as capacity is immutable.

func (*SafeInMemoryCache[K, V]) Delete

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

Delete removes a key from the cache using an exclusive write lock. Returns true if the key was found and removed, false otherwise.

func (*SafeInMemoryCache[K, V]) DeleteMany

func (c *SafeInMemoryCache[K, V]) DeleteMany(keys []K) map[K]bool

DeleteMany removes multiple keys from the cache using an exclusive write lock. Returns a map where keys are the input keys and values indicate if the key was found and removed. If the input slice is empty, returns an empty map immediately.

func (*SafeInMemoryCache[K, V]) Get

func (c *SafeInMemoryCache[K, V]) Get(key K) (value V, ok bool)

Get retrieves a value from the cache and makes it the most recently used item. Uses an exclusive write lock because the underlying cache may modify access order (e.g., move items in LRU cache), which requires write access.

func (*SafeInMemoryCache[K, V]) GetMany

func (c *SafeInMemoryCache[K, V]) GetMany(keys []K) (map[K]V, []K)

GetMany retrieves multiple values from the cache using an exclusive write lock. Returns a map of found key-value pairs and a slice of missing keys. Uses write lock because underlying cache may modify access order. If the input slice is empty, returns empty results immediately.

func (*SafeInMemoryCache[K, V]) Has

func (c *SafeInMemoryCache[K, V]) Has(key K) bool

Has checks if a key exists in the cache using a shared read lock. Multiple readers can access this method concurrently.

func (*SafeInMemoryCache[K, V]) HasMany

func (c *SafeInMemoryCache[K, V]) HasMany(keys []K) map[K]bool

HasMany checks if multiple keys exist in the cache using a shared read lock. Returns a map where keys are the input keys and values indicate existence. If the input slice is empty, returns an empty map immediately.

func (*SafeInMemoryCache[K, V]) Keys

func (c *SafeInMemoryCache[K, V]) Keys() []K

Keys returns all keys currently in the cache using a shared read lock. The returned slice is a snapshot and may not reflect concurrent modifications.

func (*SafeInMemoryCache[K, V]) Len

func (c *SafeInMemoryCache[K, V]) Len() int

Len returns the current number of items in the cache using a shared read lock. The count is accurate at the time of the lock acquisition.

func (*SafeInMemoryCache[K, V]) Peek

func (c *SafeInMemoryCache[K, V]) Peek(key K) (value V, ok bool)

Peek retrieves a value from the cache without updating access order. Uses a shared read lock since no modifications are made to the cache structure.

func (*SafeInMemoryCache[K, V]) PeekMany

func (c *SafeInMemoryCache[K, V]) PeekMany(keys []K) (map[K]V, []K)

PeekMany retrieves multiple values from the cache without updating access order. Uses a shared read lock since no modifications are made to the cache structure. Returns a map of found key-value pairs and a slice of missing keys. If the input slice is empty, returns empty results immediately.

func (*SafeInMemoryCache[K, V]) Purge

func (c *SafeInMemoryCache[K, V]) Purge()

Purge removes all keys and values from the cache using an exclusive write lock. This operation completely clears the cache and blocks all other operations.

func (*SafeInMemoryCache[K, V]) Range

func (c *SafeInMemoryCache[K, V]) Range(f func(K, V) bool)

Range iterates over all key-value pairs in the cache using a shared read lock. The iteration is performed under lock protection to ensure consistency. The iteration stops if the function returns false.

func (*SafeInMemoryCache[K, V]) Set

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

Set stores a key-value pair in the cache with exclusive write lock. This operation blocks other writers and readers until completion.

func (*SafeInMemoryCache[K, V]) SetMany

func (c *SafeInMemoryCache[K, V]) SetMany(items map[K]V)

SetMany stores multiple key-value pairs in the cache using an exclusive write lock. This is more efficient than calling Set multiple times as it uses a single lock acquisition. If the input map is empty, the operation is skipped entirely.

func (*SafeInMemoryCache[K, V]) SizeBytes added in v0.7.0

func (c *SafeInMemoryCache[K, V]) SizeBytes() int64

SizeBytes returns the total size of all cache entries in bytes using a shared read lock. The size is accurate at the time of the lock acquisition.

func (*SafeInMemoryCache[K, V]) Values

func (c *SafeInMemoryCache[K, V]) Values() []V

Values returns all values currently in the cache using a shared read lock. The returned slice is a snapshot and may not reflect concurrent modifications.

Jump to

Keyboard shortcuts

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