src

package
v0.0.0-...-882ce80 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConcurrentMapWithTTL

func NewConcurrentMapWithTTL[T any](ctx context.Context, ttl, ttlDecrement time.Duration) types.ICacheInMemory[T]

NewConcurrentMapWithTTL creates a new concurrent map with TTL support and starts a background TTL management goroutine.

func NewDynamicShardedMapWithTTL

func NewDynamicShardedMapWithTTL[T any](ctx context.Context, ttl, decrement time.Duration) types.ICacheInMemory[T]

NewDynamicShardedMapWithTTL initializes a sharded in-memory cache with TTL and decrement intervals for cleanup operations. ctx is the context that controls the lifetime of the cache and its internal operations. ttl specifies the time to live for cache entries before they expire. decrement specifies the frequency of cleanup checks for expired keys, must be less than or equal to ttl. Returns an implementation of ICacheInMemory[T].

Types

type ConcurrentMapWithTTL

type ConcurrentMapWithTTL[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ConcurrentMapWithTTL provides a thread-safe map with support for time-to-live (TTL) for its entries.

func (*ConcurrentMapWithTTL[T]) Clear

func (cMap *ConcurrentMapWithTTL[T]) Clear()

Clear removes all elements from the map and clears their associated nodes.

func (*ConcurrentMapWithTTL[T]) Delete

func (cMap *ConcurrentMapWithTTL[T]) Delete(key string)

Delete removes a key and its associated data from the map, clearing the node before deletion if it exists.

func (*ConcurrentMapWithTTL[T]) DeleteBatch

func (cMap *ConcurrentMapWithTTL[T]) DeleteBatch(keys []string)

DeleteBatch removes multiple keys and their associated data from the map. Clears each node before deletion if it exists.

func (*ConcurrentMapWithTTL[T]) Get

func (cMap *ConcurrentMapWithTTL[T]) Get(key string) (T, bool)

Get retrieves the value associated with the given key and a boolean indicating if the key exists in the map.

func (*ConcurrentMapWithTTL[T]) GetBatch

func (cMap *ConcurrentMapWithTTL[T]) GetBatch(keys []string) ([]*types.BatchNode[T], error)

GetBatch retrieves a batch of values corresponding to the provided keys from the ConcurrentMapWithTTL. It returns a slice of BatchNode containing the values, existence flags, or an error if the map is closed.

func (*ConcurrentMapWithTTL[T]) GetBatchWithMetrics

func (cMap *ConcurrentMapWithTTL[T]) GetBatchWithMetrics(keys []string) ([]*types.Metric[T], error)

GetBatchWithMetrics retrieves detailed metrics for a batch of keys, returning a slice of Metric objects or an error.

func (*ConcurrentMapWithTTL[T]) GetNodeValueWithMetrics

func (cMap *ConcurrentMapWithTTL[T]) GetNodeValueWithMetrics(key string) (T, time.Time, uint32, uint32, bool)

GetNodeValueWithMetrics retrieves the value, creation time, set count, and get count for a key, along with its existence status.

func (*ConcurrentMapWithTTL[T]) Len

func (cMap *ConcurrentMapWithTTL[T]) Len() int

Len returns the number of elements in the map. It is safe for concurrent access.

func (*ConcurrentMapWithTTL[T]) Range

func (cMap *ConcurrentMapWithTTL[T]) Range(callback func(key string, value T) bool) error

Range iterates over all key-value pairs in the map, executing the provided callback function for each pair. The iteration stops if the callback function returns false. Returns an error if the operation cannot be performed.

func (*ConcurrentMapWithTTL[T]) RangeWithMetrics

func (cMap *ConcurrentMapWithTTL[T]) RangeWithMetrics(callback func(key string, value T, createdAt time.Time, setCount uint32, getCount uint32) bool) error

func (*ConcurrentMapWithTTL[T]) Set

func (cMap *ConcurrentMapWithTTL[T]) Set(key string, value T) error

Set adds or updates a key-value pair in the map, initializing a new node if the key does not exist.

func (*ConcurrentMapWithTTL[T]) SetBatch

func (cMap *ConcurrentMapWithTTL[T]) SetBatch(batch map[string]T) error

SetBatch adds multiple key-value pairs to the map by invoking the Set method for each entry in the provided batch map.

type DynamicShardedMapWithTTL

type DynamicShardedMapWithTTL[T any] struct {
	// contains filtered or unexported fields
}

DynamicShardedMapWithTTL is a generic, sharded, in-memory cache with a configurable TTL for entries. Uses 256 shards for concurrency and stores data with automatic expiration based on the TTL settings. Allows operations like Set, Get, Delete, Clear, Len, and Range. Provides thread-safe access and automatic TTL decrement on entries to remove expired data. Each shard is lazily initialized for efficiency based on usage. Maintains internal synchronization to safely handle concurrent operations. Once closed, the map discards all shards and prevents further operations.

func (*DynamicShardedMapWithTTL[T]) Clear

func (shardMap *DynamicShardedMapWithTTL[T]) Clear()

Clear removes all data from the map, clears underlying shards, and cancels the internal context, marking the map as closed.

func (*DynamicShardedMapWithTTL[T]) Delete

func (shardMap *DynamicShardedMapWithTTL[T]) Delete(key string)

Delete removes an entry with the specified key from the map if it exists and the map is not closed.

func (*DynamicShardedMapWithTTL[T]) DeleteBatch

func (shardMap *DynamicShardedMapWithTTL[T]) DeleteBatch(keys []string)

DeleteBatch removes multiple entries from the map for the provided keys if they exist and the map is not closed.

func (*DynamicShardedMapWithTTL[T]) Get

func (shardMap *DynamicShardedMapWithTTL[T]) Get(key string) (T, bool)

Get retrieves the value associated with the given key from the dynamic sharded map and its existence status.

func (*DynamicShardedMapWithTTL[T]) GetBatch

func (shardMap *DynamicShardedMapWithTTL[T]) GetBatch(keys []string) ([]*types.BatchNode[T], error)

GetBatch retrieves a batch of key-value pairs from the map for the provided keys. Returns error if the map is closed.

func (*DynamicShardedMapWithTTL[T]) GetBatchWithMetrics

func (shardMap *DynamicShardedMapWithTTL[T]) GetBatchWithMetrics(keys []string) ([]*types.Metric[T], error)

GetBatchWithMetrics retrieves a batch of metrics for the given keys, including value, creation time, and access stats. Returns an error if the map is closed.

func (*DynamicShardedMapWithTTL[T]) GetNodeValueWithMetrics

func (shardMap *DynamicShardedMapWithTTL[T]) GetNodeValueWithMetrics(key string) (T, time.Time, uint32, uint32, bool)

GetNodeValueWithMetrics retrieves the value and associated metadata for a specific key if it exists in the map. Returns the value, the time it was created, the number of times it's been set, the number of times it's been retrieved, and a boolean indicating existence.

func (*DynamicShardedMapWithTTL[T]) Len

func (shardMap *DynamicShardedMapWithTTL[T]) Len() int

Len returns the total number of entries across all shards in the map or 0 if the map is closed.

func (*DynamicShardedMapWithTTL[T]) Range

func (shardMap *DynamicShardedMapWithTTL[T]) Range(callback func(key string, value T) bool) error

Range iterates over all key-value pairs in the map, applying the provided callback function. Returns an error if the map is closed or if an error occurs during shard iteration.

func (*DynamicShardedMapWithTTL[T]) RangeWithMetrics

func (shardMap *DynamicShardedMapWithTTL[T]) RangeWithMetrics(callback func(key string, value T, createdAt time.Time, setCount uint32, getCount uint32) bool) error

func (*DynamicShardedMapWithTTL[T]) Set

func (shardMap *DynamicShardedMapWithTTL[T]) Set(key string, value T) error

Set inserts or updates a key-value pair in the dynamic sharded map. Returns an error if the map is closed.

func (*DynamicShardedMapWithTTL[T]) SetBatch

func (shardMap *DynamicShardedMapWithTTL[T]) SetBatch(batch map[string]T) error

SetBatch inserts or updates multiple key-value pairs in the dynamic sharded map and returns an error if the map is closed.

type IMapNode

type IMapNode[T any] interface {
	SetTTL(ttl time.Duration)
	SetTTLDecrement(ttlDecrement time.Duration)
	SetRemoveCallback(remove func())
	Tick()
	GetData() T
	SetData(data T)
	Clear()
	GetDataWithMetrics() (T, time.Time, uint32, uint32)
}

IMapNode represents an interface for managing nodes with TTL, data handling, lifecycle control, and metrics tracking.

type MapNode

type MapNode[T any] struct {
	// contains filtered or unexported fields
}

MapNode represents a generic node with a time-to-live (TTL) mechanism, handling data, lifecycle, and expiration.

func NewMapNode

func NewMapNode[T any](data T) *MapNode[T]

NewMapNode creates a new instance of a MapNode with the given data, returning it as an implementation of IMapNode.

func (*MapNode[T]) Clear

func (node *MapNode[T]) Clear()

Clear resets all fields of the MapNode to their zero values, effectively clearing its state and binding.

func (*MapNode[T]) GetData

func (node *MapNode[T]) GetData() T

GetData resets the node's duration to its ttl value and returns the data stored in the node.

func (*MapNode[T]) GetDataWithMetrics

func (node *MapNode[T]) GetDataWithMetrics() (T, time.Time, uint32, uint32)

GetMetrics returns the creation time, set count, and get count for the MapNode instance.

func (*MapNode[T]) IsDeleted

func (node *MapNode[T]) IsDeleted() bool

func (*MapNode[T]) SetData

func (node *MapNode[T]) SetData(data T)

SetData sets the data for the MapNode instance.

func (*MapNode[T]) SetRemoveCallback

func (node *MapNode[T]) SetRemoveCallback(remove func())

SetRemoveCallback sets the callback function to be invoked when the node needs to be removed due to TTL expiration.

func (*MapNode[T]) SetTTL

func (node *MapNode[T]) SetTTL(ttl time.Duration)

SetTTL sets the time-to-live (TTL) duration for the MapNode instance.

func (*MapNode[T]) SetTTLDecrement

func (node *MapNode[T]) SetTTLDecrement(ttlDecrement time.Duration)

SetTTLDecrement sets the time duration to decrement from the node's TTL on each tick operation.

func (*MapNode[T]) Tick

func (node *MapNode[T]) Tick()

Tick decreases the node's remaining time-to-live by the decrement value and invokes the removal callback if expired.

Jump to

Keyboard shortcuts

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