collections

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package collections provides generic, thread-safe data structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentMap

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

ConcurrentMap is a generic, thread-safe map implementation. It uses an internal sync.RWMutex to provide concurrent access safety.

Type parameters:

  • K must be comparable (usable as a map key)
  • V can be any type

Example usage:

cm := collections.NewConcurrentMap[string, *TokenRecord]()
cm.Set("token123", record)
value, ok := cm.Get("token123")
cm.Delete("token123")

ConcurrentMap is safe for concurrent use by multiple goroutines. All operations are atomic and protected by the internal mutex.

func NewConcurrentMap

func NewConcurrentMap[K comparable, V any]() *ConcurrentMap[K, V]

NewConcurrentMap creates a new empty ConcurrentMap.

func (*ConcurrentMap[K, V]) Clear

func (m *ConcurrentMap[K, V]) Clear()

Clear removes all entries from the map.

This operation acquires a write lock.

func (*ConcurrentMap[K, V]) CompareAndDelete

func (m *ConcurrentMap[K, V]) CompareAndDelete(key K, expected V) bool

CompareAndDelete deletes a key only if its value matches the expected value. Returns true if the key was deleted, false otherwise.

IMPORTANT: This uses direct == comparison after type erasure via any(). - Works correctly for: basic types (int, string, bool), pointers - Does NOT work reliably for: structs, slices, maps, arrays - For complex types, use CompareAndDeleteFunc() instead.

This operation acquires a write lock.

func (*ConcurrentMap[K, V]) CompareAndDeleteFunc

func (m *ConcurrentMap[K, V]) CompareAndDeleteFunc(key K, expected V, equal func(a, b V) bool) bool

CompareAndDeleteFunc deletes a key only if its value matches according to the provided equality function. Returns true if the key was deleted, false otherwise.

This variant allows custom comparison logic for complex types like structs, where deep equality checking is required.

Example:

type Record struct { ID string; Data int }
equal := func(a, b Record) bool { return a.ID == b.ID && a.Data == b.Data }
cm.CompareAndDeleteFunc("key", expected, equal)

This operation acquires a write lock.

func (*ConcurrentMap[K, V]) Delete

func (m *ConcurrentMap[K, V]) Delete(key K)

Delete removes a key from the map. If the key doesn't exist, this is a no-op.

This operation acquires a write lock, blocking other writes and reads.

func (*ConcurrentMap[K, V]) ForEach

func (m *ConcurrentMap[K, V]) ForEach(fn func(key K, value V) bool)

ForEach applies a function to each key-value pair in the map. The callback receives a point-in-time snapshot of the map, so it's safe to call other ConcurrentMap methods from within the callback without deadlock.

Note: The callback sees a snapshot taken at the time ForEach is called. Concurrent modifications during iteration won't be visible to the callback.

If the function returns false, iteration stops early.

This operation copies the map entries, so it uses O(n) memory.

func (*ConcurrentMap[K, V]) Get

func (m *ConcurrentMap[K, V]) Get(key K) (V, bool)

Get retrieves a value from the map. Returns the value and true if the key exists, zero value and false otherwise.

This operation acquires a read lock, allowing multiple concurrent reads.

func (*ConcurrentMap[K, V]) GetOrSet

func (m *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, loaded bool)

GetOrSet atomically gets a value if it exists, or sets and returns the provided value. Returns the value (either existing or newly set) and true if the value was already present.

This operation acquires a write lock.

func (*ConcurrentMap[K, V]) Has

func (m *ConcurrentMap[K, V]) Has(key K) bool

Has checks if a key exists in the map.

This operation acquires a read lock.

func (*ConcurrentMap[K, V]) Keys

func (m *ConcurrentMap[K, V]) Keys() []K

Keys returns a slice of all keys in the map. The returned slice is a snapshot; modifications to the map won't affect it.

This operation acquires a read lock for the duration of the key copy.

func (*ConcurrentMap[K, V]) Len

func (m *ConcurrentMap[K, V]) Len() int

Len returns the number of items in the map.

This operation acquires a read lock.

func (*ConcurrentMap[K, V]) Set

func (m *ConcurrentMap[K, V]) Set(key K, value V)

Set stores a key-value pair in the map. If the key already exists, its value is updated.

This operation acquires a write lock, blocking other writes and reads.

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache is a thread-safe, fixed-size LRU cache.

func NewLRUCache

func NewLRUCache(capacity int) *LRUCache

NewLRUCache creates a new LRUCache with the given capacity.

func (*LRUCache) Delete

func (c *LRUCache) Delete(key interface{})

Delete removes an entry from the cache.

func (*LRUCache) Get

func (c *LRUCache) Get(key interface{}) (interface{}, bool)

Get retrieves a value from the cache.

func (*LRUCache) Len

func (c *LRUCache) Len() int

Len returns the number of items in the cache.

func (*LRUCache) Put

func (c *LRUCache) Put(key, value interface{})

Put adds a value to the cache.

func (*LRUCache) Range

func (c *LRUCache) Range(f func(key, value interface{}) bool)

Range iterates over the cache and calls the given function for each key and value.

Jump to

Keyboard shortcuts

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