generic_cache

package
v0.0.0-...-eec02a6 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiValueCache

type MultiValueCache[K comparable, V comparable] struct {
	// contains filtered or unexported fields
}

MultiValueCache implements the Cache[K, V] interface for many-to-many relationships with idempotent operations and O(1) performance for ALL operations in BOTH directions.

Key features: - Idempotent Set: Adding the same key-value pair multiple times has no effect - O(1) - O(1) Delete: Uses swap-and-pop technique for constant-time deletion in both directions - O(1) Get: Returns pre-computed array copy (no iteration) - O(1) FindKeyForValue: Returns pre-computed array copy (symmetric with Get) - Fully symmetric: Both forward (K→V) and reverse (V→K) use same optimization - Thread-safe: Uses RWMutex for concurrent access

Data structures (fully symmetric): Forward mapping: - mapping: map[K]map[V]int stores the index of each value in arrays[K] - arrays: map[K][]V cached arrays for instant Get(K) returns

Reverse mapping (symmetric!): - valueToKeys: map[V]map[K]int stores the index of each key in reverseArrays[V] - reverseArrays: map[V][]K cached arrays for instant FindKeyForValue(V) returns

The key optimization is storing array indices in both mapping and valueToKeys: - Set: O(1) - append to both arrays, store indices in both maps - Delete: O(1) - swap-and-pop in both directions, update both indices - Get: O(1) - return cached array copy from arrays - FindKeyForValue: O(1) - return cached array copy from reverseArrays

Example use case: Many-to-many relationships like Tags → Resources - One tag can have multiple resources - One resource can have multiple tags - Need fast lookup in both directions

func NewMultiValueCache

func NewMultiValueCache[K comparable, V comparable]() *MultiValueCache[K, V]

NewMultiValueCache creates a new idempotent multi-value cache.

func (*MultiValueCache[K, V]) Clear

func (c *MultiValueCache[K, V]) Clear()

Clear removes all entries from the cache.

func (*MultiValueCache[K, V]) Delete

func (c *MultiValueCache[K, V]) Delete(key K, value V) error

Delete removes a specific key-value pair from the cache. Idempotent: Removing a non-existent pair is a no-op. O(1) operation using swap-and-pop technique in BOTH directions: Forward: Remove value from key's array Reverse: Remove key from value's array

func (*MultiValueCache[K, V]) DeleteKey

func (c *MultiValueCache[K, V]) DeleteKey(key K) error

DeleteKey removes all values for a key. Also cleans up reverse mappings for all affected values. O(n) where n = number of values for this key (typically small).

func (*MultiValueCache[K, V]) FindKeyForValue

func (c *MultiValueCache[K, V]) FindKeyForValue(value V) []K

FindKeyForValue returns all keys that contain the given value. O(1) operation - returns cached array copy (symmetric with Get). Supports many-to-many relationships where one value can belong to multiple keys.

func (*MultiValueCache[K, V]) Get

func (c *MultiValueCache[K, V]) Get(key K) []V

Get returns all values for a key as a defensive copy. O(1) operation - returns cached array copy (no iteration needed).

func (*MultiValueCache[K, V]) Has

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

Has checks if a key exists in the cache. O(1) operation.

func (*MultiValueCache[K, V]) Len

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

Len returns the number of keys in the cache. O(1) operation.

func (*MultiValueCache[K, V]) List

func (c *MultiValueCache[K, V]) List() map[K][]V

List returns all key-value pairs in the cache as a defensive copy. Returns map[K][]V with copies of all arrays.

func (*MultiValueCache[K, V]) Set

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

Set adds a value to the set of values for a key. Returns error if the same key-value pair already exists. O(1) operation - updates both forward and reverse mappings.

type SingleValueCache

type SingleValueCache[K comparable, V comparable] struct {
	// contains filtered or unexported fields
}

SingleValueCache implements the Cache[K, V] interface (defined in parent cache package) for one-to-one relationships with generic types. K must be comparable (suitable for map keys). V must also be comparable (for value lookup). Example: Volume (string) → PolicyName (string), PVC (string) → SnapshotName (string) Thread-safe implementation using RWMutex. Maintains a reverse index (valueToKeys) for O(1) value lookups.

func NewSingleValueCache

func NewSingleValueCache[K comparable, V comparable]() *SingleValueCache[K, V]

NewSingleValueCache creates a new single-value cache.

func (*SingleValueCache[K, V]) Clear

func (c *SingleValueCache[K, V]) Clear()

Clear removes all entries from the cache.

func (*SingleValueCache[K, V]) Delete

func (c *SingleValueCache[K, V]) Delete(key K, value V) error

Delete removes a key from the cache. The value parameter is ignored for single-value caches.

func (*SingleValueCache[K, V]) DeleteKey

func (c *SingleValueCache[K, V]) DeleteKey(key K) error

DeleteKey removes a key from the cache. This is the same as Delete for single-value caches.

func (*SingleValueCache[K, V]) FindKeyForValue

func (c *SingleValueCache[K, V]) FindKeyForValue(value V) []K

FindKeyForValue searches for all keys that map to the given value. Returns slice with all matching keys, empty slice if none found. This is an O(1) operation using the reverse index. Note: Multiple keys can map to the same value (many-to-one). Returns a defensive copy to prevent external mutation.

func (*SingleValueCache[K, V]) Get

func (c *SingleValueCache[K, V]) Get(key K) []V

Get returns the value for a key as a single-element slice. Returns empty slice if key doesn't exist.

func (*SingleValueCache[K, V]) Has

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

Has checks if a key exists in the cache.

func (*SingleValueCache[K, V]) Len

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

Len returns the number of keys in the cache.

func (*SingleValueCache[K, V]) List

func (c *SingleValueCache[K, V]) List() map[K][]V

List returns all key-value pairs in the cache as a defensive copy. Returns map[K][]V with each single value wrapped in a slice for consistency with the interface.

func (*SingleValueCache[K, V]) Set

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

Set stores a single value for a key. Returns error if the same key-value pair already exists. If the key exists with a different value, it replaces the old value. Key must not be zero value. Value can be any valid V type (including zero).

Jump to

Keyboard shortcuts

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