Documentation
¶
Index ¶
- type BaseCache
- type Cache
- type ComparableCache
- type ComparableKey
- type ComparableSlice
- type CompositeKey
- type FarmHash64CompositeKey
- type FarmHash64Entity
- type GenericCompositeKey
- type InMemoryBufferedComparableMapCache
- func (c *InMemoryBufferedComparableMapCache[K, T]) Changes() []K
- func (c *InMemoryBufferedComparableMapCache[K, T]) CloseBuffered()
- func (c *InMemoryBufferedComparableMapCache[K, T]) Drop()
- func (c *InMemoryBufferedComparableMapCache[K, T]) DropKey(key K)
- func (c *InMemoryBufferedComparableMapCache[K, T]) Get(key K) (*T, bool)
- func (c *InMemoryBufferedComparableMapCache[K, T]) GetValue(key K) (T, bool)
- func (c *InMemoryBufferedComparableMapCache[K, T]) Keys() []K
- func (c *InMemoryBufferedComparableMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (c *InMemoryBufferedComparableMapCache[K, T]) RemoveOutdated() int
- func (c *InMemoryBufferedComparableMapCache[K, T]) Set(key K, value T)
- func (c *InMemoryBufferedComparableMapCache[K, T]) SetQuietly(key K, value T)
- func (c *InMemoryBufferedComparableMapCache[K, T]) SetTTL(ttl uopt.Opt[time.Duration])
- func (c *InMemoryBufferedComparableMapCache[K, T]) Wait()
- type InMemoryComparableMapCache
- func (c *InMemoryComparableMapCache[K, T]) Changes() []K
- func (c *InMemoryComparableMapCache[K, T]) Drop()
- func (c *InMemoryComparableMapCache[K, T]) DropKey(key K)
- func (c *InMemoryComparableMapCache[K, T]) Get(key K) (*T, bool)
- func (c *InMemoryComparableMapCache[K, T]) GetValue(key K) (T, bool)
- func (c *InMemoryComparableMapCache[K, T]) Keys() []K
- func (c *InMemoryComparableMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (c *InMemoryComparableMapCache[K, T]) RemoveOutdated() int
- func (c *InMemoryComparableMapCache[K, T]) Set(key K, value T)
- func (c *InMemoryComparableMapCache[K, T]) SetQuietly(key K, value T)
- func (c *InMemoryComparableMapCache[K, T]) SetTTL(ttl uopt.Opt[time.Duration])
- type InMemoryComparableMapCacheOptions
- type InMemoryHashMapCache
- func (c *InMemoryHashMapCache[K, T]) Changes() []K
- func (c *InMemoryHashMapCache[K, T]) Drop()
- func (c *InMemoryHashMapCache[K, T]) DropKey(key K)
- func (c *InMemoryHashMapCache[K, T]) Get(key K) (*T, bool)
- func (c *InMemoryHashMapCache[K, T]) GetValue(key K) (T, bool)
- func (c *InMemoryHashMapCache[K, T]) Keys() []K
- func (c *InMemoryHashMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (c *InMemoryHashMapCache[K, T]) RemoveOutdated() int
- func (c *InMemoryHashMapCache[K, T]) Set(key K, value T)
- func (c *InMemoryHashMapCache[K, T]) SetQuietly(key K, value T)
- func (c *InMemoryHashMapCache[K, T]) SetTTL(ttl uopt.Opt[time.Duration])
- type InMemoryHashMapMultiCache
- func NewDefaultHashMapMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, uint64]
- func NewFarmHashMapMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, uint64]
- func NewInMemoryHashMapMultiCache[K CompositeKey, T any, H comparable](toHash func(keys []uconst.Unique) H, ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, H]
- func (c *InMemoryHashMapMultiCache[K, T, H]) Changes() []K
- func (c *InMemoryHashMapMultiCache[K, T, H]) Drop()
- func (c *InMemoryHashMapMultiCache[K, T, H]) DropKey(key K)
- func (c *InMemoryHashMapMultiCache[K, T, H]) Get(key K) []T
- func (c *InMemoryHashMapMultiCache[K, T, H]) Keys() []K
- func (c *InMemoryHashMapMultiCache[K, T, H]) Outdated(key uopt.Opt[K]) bool
- func (c *InMemoryHashMapMultiCache[K, T, H]) Put(key K, values ...T)
- func (c *InMemoryHashMapMultiCache[K, T, H]) PutQuietly(key K, values ...T)
- func (c *InMemoryHashMapMultiCache[K, T, H]) RemoveOutdated() int
- func (c *InMemoryHashMapMultiCache[K, T, H]) Set(key K, values ...T)
- func (c *InMemoryHashMapMultiCache[K, T, H]) SetTTL(ttl uopt.Opt[time.Duration])
- type InMemoryTreeMultiCache
- func (c *InMemoryTreeMultiCache[K, T]) Changes() []K
- func (c *InMemoryTreeMultiCache[K, T]) Drop()
- func (c *InMemoryTreeMultiCache[K, T]) DropKey(key K)
- func (c *InMemoryTreeMultiCache[K, T]) Get(key K) []T
- func (c *InMemoryTreeMultiCache[K, T]) Keys() []K
- func (c *InMemoryTreeMultiCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (c *InMemoryTreeMultiCache[K, T]) Put(key K, val ...T)
- func (c *InMemoryTreeMultiCache[K, T]) PutQuietly(key K, val ...T)
- func (c *InMemoryTreeMultiCache[K, T]) Set(key K, val ...T)
- func (c *InMemoryTreeMultiCache[K, T]) SetTTL(ttl uopt.Opt[time.Duration])
- type Int64Value
- type IntCompositeKey
- type IntKey
- type ManagedCache
- func (b *ManagedCache[K, T]) Changes() []K
- func (b *ManagedCache[K, T]) Drop()
- func (b *ManagedCache[K, T]) DropKey(key K)
- func (b *ManagedCache[K, T]) ForceCleanup()
- func (b *ManagedCache[K, T]) Get(key K) (*T, bool)
- func (b *ManagedCache[K, T]) GetValue(key K) (T, bool)
- func (b *ManagedCache[K, T]) Keys() []K
- func (b *ManagedCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (b *ManagedCache[K, T]) Set(key K, value T)
- func (b *ManagedCache[K, T]) SetQuietly(key K, value T)
- func (b *ManagedCache[K, T]) Stop()
- type ManagedCacheOptions
- type ManagedMultiCache
- func (b *ManagedMultiCache[K, T]) Changes() []K
- func (b *ManagedMultiCache[K, T]) Drop()
- func (b *ManagedMultiCache[K, T]) DropKey(key K)
- func (b *ManagedMultiCache[K, T]) ForceCleanup()
- func (b *ManagedMultiCache[K, T]) Get(key K) []T
- func (b *ManagedMultiCache[K, T]) Outdated(key uopt.Opt[K]) bool
- func (b *ManagedMultiCache[K, T]) Put(key K, values ...T)
- func (b *ManagedMultiCache[K, T]) PutQuietly(key K, values ...T)
- func (b *ManagedMultiCache[K, T]) Set(key K, values ...T)
- func (b *ManagedMultiCache[K, T]) Stop()
- type MultiCache
- type StrCompositeKey
- type StringKey
- type StringSliceValue
- type StringValue
- type UIntCompositeKey
- type UIntKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseCache ¶
type BaseCache[K, T any] interface { // Set updates the cache value for the provided key. If the key already exists, // its previous value is removed before adding the new value. This method should be thread-safe. Set(key K, value T) // Get retrieves the value associated with the provided key from the cache. // It returns the value and a boolean indicating whether the key was found. // This method should be thread-safe. Get operation drops down change state of the item, meaning that item becomes // actual after Get operation. Get(key K) (*T, bool) // GetValue retrieves the value associated with the provided key without allocating a pointer result. GetValue(key K) (T, bool) // Changes returns a slice of keys that have been modified in the cache. // This method provides a way to track changes made to the cache, useful for scenarios like cache syncing. // Cache changes will be updated only on modifying operations, but not on Drop() call, meaning that in-fact, changes contain all the present keys. Changes() []K // Keys returns a snapshot of all currently stored keys. // This method is used by managed cleanup and should not depend on change tracking. Keys() []K // Drop completely clears the cache, removing all entries. This method should be thread-safe. Drop() // DropKey removes the value associated with the provided key from the cache. This method should be thread-safe. // This method also clears up changes associated with this key. DropKey(key K) // Outdated checks if the provided key or the entire cache (if no key is provided) // is outdated based on the set TTL (time-to-live). Returns true if outdated, false otherwise. // This method should be thread-safe. // If key was not found returns false. Outdated(key uopt.Opt[K]) bool // SetQuietly is an optimized method adds a value to the cache for the provided key but does so without // altering the change history. This method is useful when modifications should not trigger cache change diff. // This method should be thread-safe. // This operation is much faster and can be used to optimize cache performance in case you don't want to track changes. SetQuietly(key K, value T) }
type Cache ¶
The Cache interface defines a set of methods for a generic cache implementation. This interface supports setting, getting, and managing cache entries with composite keys. Unlike MultiCache, it is designed to handle only one value per key and does not support hierarchical composite keys.
type ComparableCache ¶
type ComparableCache[K comparable, T any] interface { BaseCache[K, T] }
The ComparableCache is the same as Cache, but is more generic and allows comparable keys.
type ComparableKey ¶
type ComparableKey[T comparable] struct { // contains filtered or unexported fields }
func NewComparableKey ¶
func NewComparableKey[T comparable](v T) ComparableKey[T]
func (ComparableKey[T]) Equals ¶
func (k ComparableKey[T]) Equals(other uconst.Comparable) bool
func (ComparableKey[T]) Key ¶
func (k ComparableKey[T]) Key() int64
func (ComparableKey[T]) String ¶
func (k ComparableKey[T]) String() string
type ComparableSlice ¶
type ComparableSlice[T uconst.Comparable] struct { Data []T }
func (ComparableSlice[T]) Equals ¶
func (c ComparableSlice[T]) Equals(other uconst.Comparable) bool
type CompositeKey ¶
type CompositeKey interface {
uconst.Comparable
Keys() []uconst.Unique // Keys returns an ordered list of keys ordered by priority (ASC), so the first element has the most prio.
}
CompositeKey specifies an abstract key with an ability to provide an ordered list of available keys.
type FarmHash64CompositeKey ¶
type FarmHash64CompositeKey struct {
// contains filtered or unexported fields
}
func NewFarmHashCompositeKey ¶
func NewFarmHashCompositeKey(keys ...any) FarmHash64CompositeKey
NewFarmHashCompositeKey creates a GenericCompositeKey with Farm64 support.
func (FarmHash64CompositeKey) Equals ¶
func (k FarmHash64CompositeKey) Equals(other uconst.Comparable) bool
func (FarmHash64CompositeKey) Keys ¶
func (k FarmHash64CompositeKey) Keys() []uconst.Unique
type FarmHash64Entity ¶
type FarmHash64Entity struct {
// contains filtered or unexported fields
}
FarmHash64Entity wraps any object and provides a uconst.Unique implementation using farm's 64-bit hash function to be used in cache. This hashed entity uses internal hash to avoid redundant rehashing operations.
IMPORTANT: The object must have exported fields and only those fields will be considered for the hashing uniqueness operation. IMPORTANT: If the object is a pointer, the hash will compare pointer values. If the object is not a pointer, the hash will compare contents.
- Equals method compares the hash values of the wrapped objects.
- Key method uses farm.Hash64 to generate a 64-bit hash of the object and returns it as an int64 value for the key.
This can be used for uniquely identifying objects and comparing them based on their content rather than their memory address.
func Hashed ¶
func Hashed(obj any) FarmHash64Entity
Hashed is a constructor function that creates and returns a new instance of FarmHash64Entity, wrapping the provided object. This instance provides a uconst.Unique implementation using farm's 64-bit hash function.
Usage:
- To uniquely identify objects based on their content rather than their memory address.
- To generate a unique key for any object by hashing its content.
Example:
obj := Hashed("example object")
fmt.Println("Key:", obj.Key())
// Outputs the unique key generated by farm.Hash64 for the string "example object".
func (FarmHash64Entity) Equals ¶
func (e FarmHash64Entity) Equals(other uconst.Comparable) bool
func (FarmHash64Entity) Key ¶
func (e FarmHash64Entity) Key() int64
type GenericCompositeKey ¶
type GenericCompositeKey struct {
// contains filtered or unexported fields
}
func NewGenericCompositeKey ¶
func NewGenericCompositeKey(keys ...any) GenericCompositeKey
NewGenericCompositeKey creates a GenericCompositeKey that supports only 'comparable' keys
func (GenericCompositeKey) Equals ¶
func (k GenericCompositeKey) Equals(other uconst.Comparable) bool
func (GenericCompositeKey) Keys ¶
func (k GenericCompositeKey) Keys() []uconst.Unique
func (GenericCompositeKey) String ¶
func (k GenericCompositeKey) String() string
type InMemoryBufferedComparableMapCache ¶
type InMemoryBufferedComparableMapCache[K comparable, T any] struct { // contains filtered or unexported fields }
InMemoryBufferedComparableMapCache wraps InMemoryComparableMapCache and uses buffered writes by default. Set and SetQuietly enqueue writes and return immediately. Call Wait to make accepted writes visible deterministically. If BufferedMaxKeys is configured, new keys above the limit can be rejected silently by Set and SetQuietly.
func NewInMemoryBufferedComparableMapCache ¶
func NewInMemoryBufferedComparableMapCache[K comparable, T any]( ttl uopt.Opt[time.Duration], ) *InMemoryBufferedComparableMapCache[K, T]
NewInMemoryBufferedComparableMapCache creates a buffered comparable-key cache with default buffered write settings.
func NewInMemoryBufferedComparableMapCacheWithOptions ¶
func NewInMemoryBufferedComparableMapCacheWithOptions[K comparable, T any]( options InMemoryComparableMapCacheOptions, ) *InMemoryBufferedComparableMapCache[K, T]
NewInMemoryBufferedComparableMapCacheWithOptions creates a buffered comparable-key cache with custom settings.
func (*InMemoryBufferedComparableMapCache[K, T]) Changes ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Changes() []K
func (*InMemoryBufferedComparableMapCache[K, T]) CloseBuffered ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) CloseBuffered()
CloseBuffered waits for buffered updates and stops background buffer workers. After CloseBuffered, Set and SetQuietly stop accepting buffered writes.
func (*InMemoryBufferedComparableMapCache[K, T]) Drop ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Drop()
func (*InMemoryBufferedComparableMapCache[K, T]) DropKey ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) DropKey(key K)
func (*InMemoryBufferedComparableMapCache[K, T]) Get ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Get(key K) (*T, bool)
func (*InMemoryBufferedComparableMapCache[K, T]) GetValue ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) GetValue(key K) (T, bool)
func (*InMemoryBufferedComparableMapCache[K, T]) Keys ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Keys() []K
func (*InMemoryBufferedComparableMapCache[K, T]) Outdated ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
func (*InMemoryBufferedComparableMapCache[K, T]) RemoveOutdated ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) RemoveOutdated() int
func (*InMemoryBufferedComparableMapCache[K, T]) Set ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Set(key K, value T)
func (*InMemoryBufferedComparableMapCache[K, T]) SetQuietly ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) SetQuietly(key K, value T)
func (*InMemoryBufferedComparableMapCache[K, T]) SetTTL ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) SetTTL(ttl uopt.Opt[time.Duration])
func (*InMemoryBufferedComparableMapCache[K, T]) Wait ¶
func (c *InMemoryBufferedComparableMapCache[K, T]) Wait()
Wait blocks until all currently accepted buffered updates are applied.
type InMemoryComparableMapCache ¶
type InMemoryComparableMapCache[K comparable, T any] struct { // contains filtered or unexported fields }
InMemoryComparableMapCache provides an in-memory caching mechanism for single-value entries. It supports optional TTL for entries and ensures concurrency-safe operations. It is very similiar to InMemoryHashMapCache by behaviour, and the only difference is a key type constraint.
func NewInMemoryComparableMapCache ¶
func NewInMemoryComparableMapCache[K comparable, T any](ttl uopt.Opt[time.Duration]) *InMemoryComparableMapCache[K, T]
NewInMemoryComparableMapCache creates a new instance of InMemoryComparableMapCache. It accepts an optional TTL (time-to-live) duration for cache entries.
func NewInMemoryComparableMapCacheWithOptions ¶
func NewInMemoryComparableMapCacheWithOptions[K comparable, T any]( options InMemoryComparableMapCacheOptions, ) *InMemoryComparableMapCache[K, T]
NewInMemoryComparableMapCacheWithOptions creates a new InMemoryComparableMapCache with TTL and buffered write options.
func (*InMemoryComparableMapCache[K, T]) Changes ¶
func (c *InMemoryComparableMapCache[K, T]) Changes() []K
Changes returns a slice of keys that have been modified in the cache since the last call to Changes. This method is thread-safe.
func (*InMemoryComparableMapCache[K, T]) Drop ¶
func (c *InMemoryComparableMapCache[K, T]) Drop()
Drop completely clears the cache, removing all entries. The operation is thread-safe.
func (*InMemoryComparableMapCache[K, T]) DropKey ¶
func (c *InMemoryComparableMapCache[K, T]) DropKey(key K)
DropKey removes the value associated with the provided key from the cache. The operation is thread-safe.
func (*InMemoryComparableMapCache[K, T]) Get ¶
func (c *InMemoryComparableMapCache[K, T]) Get(key K) (*T, bool)
Get retrieves the value associated with the provided key from the cache. It returns a pointer to the value and a boolean indicating whether the key was found. The operation is thread-safe.
func (*InMemoryComparableMapCache[K, T]) GetValue ¶
func (c *InMemoryComparableMapCache[K, T]) GetValue(key K) (T, bool)
func (*InMemoryComparableMapCache[K, T]) Keys ¶
func (c *InMemoryComparableMapCache[K, T]) Keys() []K
func (*InMemoryComparableMapCache[K, T]) Outdated ¶
func (c *InMemoryComparableMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
Outdated checks if the provided key is outdated based on the set TTL (time-to-live). Returns true if outdated, false otherwise. If no TTL is set, it returns false. If TTL is set and the key does not exist, it returns true.
func (*InMemoryComparableMapCache[K, T]) RemoveOutdated ¶
func (c *InMemoryComparableMapCache[K, T]) RemoveOutdated() int
func (*InMemoryComparableMapCache[K, T]) Set ¶
func (c *InMemoryComparableMapCache[K, T]) Set(key K, value T)
Set updates the cache value for the provided key. If the key already exists, its previous value is replaced with the new value. The operation is thread-safe.
func (*InMemoryComparableMapCache[K, T]) SetQuietly ¶
func (c *InMemoryComparableMapCache[K, T]) SetQuietly(key K, value T)
SetQuietly adds a value to the cache for the provided key without altering the change history. This method is thread-safe and optimized for performance when change tracking is unnecessary.
type InMemoryComparableMapCacheOptions ¶
type InMemoryComparableMapCacheOptions struct {
// TTL configures cache entry time-to-live.
TTL uopt.Opt[time.Duration]
// BufferedWorkers configures how many workers apply buffered writes.
BufferedWorkers int
// BufferedQueueSize configures the buffered write queue capacity.
BufferedQueueSize int
// BufferedMaxKeys limits keys accepted through buffered writes. Zero means unlimited.
// Synchronous Set and SetQuietly keep exact write semantics and are not rejected by this limit.
BufferedMaxKeys int64
}
type InMemoryHashMapCache ¶
type InMemoryHashMapCache[K uconst.Unique, T any] struct { // contains filtered or unexported fields }
InMemoryHashMapCache provides an in-memory caching mechanism using hashmaps for single-value entries. Unlike InMemoryHashMapMultiCache, it stores only one value per key. This implementation supports linked-chain collision resolution, so at the worst it should be O(n) complexity. This structure translates composite keys into a hash value using a user-provided hashing function. Supports optional TTL for entries and ensures concurrency-safe operations using a mutex. TTL parameter in cache doesn't automatically clean up all the entries. Use ManagedCache wrapper to automatically manage outdated keys.
func NewInMemoryHashMapCache ¶
func NewInMemoryHashMapCache[K uconst.Unique, T any](ttl uopt.Opt[time.Duration]) *InMemoryHashMapCache[K, T]
NewInMemoryHashMapCache creates a new instance of the InMemoryHashMapCache. It takes a hashing function to translate the composite keys to a desired hash type, and an optional time-to-live duration for the cache entries.
func (*InMemoryHashMapCache[K, T]) Changes ¶
func (c *InMemoryHashMapCache[K, T]) Changes() []K
Changes returns a slice of keys that have been modified in the cache. This method provides a way to track changes made to the cache, useful for scenarios like cache syncing.
func (*InMemoryHashMapCache[K, T]) Drop ¶
func (c *InMemoryHashMapCache[K, T]) Drop()
Drop completely clears the cache, removing all entries. The operation is thread-safe.
func (*InMemoryHashMapCache[K, T]) DropKey ¶
func (c *InMemoryHashMapCache[K, T]) DropKey(key K)
DropKey removes the value associated with the provided key from the cache. The operation is thread-safe.
func (*InMemoryHashMapCache[K, T]) Get ¶
func (c *InMemoryHashMapCache[K, T]) Get(key K) (*T, bool)
Get retrieves the value associated with the provided key from the cache. The operation is thread-safe and does not alter the change history.
func (*InMemoryHashMapCache[K, T]) GetValue ¶
func (c *InMemoryHashMapCache[K, T]) GetValue(key K) (T, bool)
func (*InMemoryHashMapCache[K, T]) Keys ¶
func (c *InMemoryHashMapCache[K, T]) Keys() []K
func (*InMemoryHashMapCache[K, T]) Outdated ¶
func (c *InMemoryHashMapCache[K, T]) Outdated(key uopt.Opt[K]) bool
Outdated checks if the provided key or the entire cache (if no key is provided) is outdated based on the set TTL. Returns true if outdated, false otherwise.
func (*InMemoryHashMapCache[K, T]) RemoveOutdated ¶
func (c *InMemoryHashMapCache[K, T]) RemoveOutdated() int
func (*InMemoryHashMapCache[K, T]) Set ¶
func (c *InMemoryHashMapCache[K, T]) Set(key K, value T)
Set updates the cache value for the provided key. If the key already exists, its previous value are removed before adding the new value. The operation is thread-safe.
func (*InMemoryHashMapCache[K, T]) SetQuietly ¶
func (c *InMemoryHashMapCache[K, T]) SetQuietly(key K, value T)
SetQuietly is an optimized method that adds value to the cache for the provided key but does so without altering the change history. This operation can be used when modifications should not trigger cache change diff. This operation is much faster and can be used to optimize cache performance in case you don't want to track changes.
type InMemoryHashMapMultiCache ¶
type InMemoryHashMapMultiCache[K CompositeKey, T any, H comparable] struct { // contains filtered or unexported fields }
InMemoryHashMapMultiCache provides an in-memory caching mechanism using hashmaps. Unlike InMemoryHashMapCache, it stores multiple values per key. This cache structure translates composite keys into a hash value using a user-provided hashing function. The cache supports optional TTL (time-to-live) for entries (Please read below regarding clean up.) Concurrency-safe operations are ensured through the use of a mutex. Unlike InMemoryTreeMultiCache, it doesn't support a hierarchy for keys, so each key is unique. - Setting a more specific key (e.g., [1, 2, 3, 4]) WILL NOT replace the value of a broader key (e.g., [1, 2, 3]). - Deleting a specific key or a parent key (e.g., [1, 2]) will not affect the other one.
Performance Comparison with InMemoryTreeMultiCache: - Insertions: InMemoryTreeMultiCache is slightly faster for single-depth insertions and significantly faster for deeper depths. - Retrievals: InMemoryHashMapMultiCache (especially with FarmHash) is faster for both single-depth and deeper retrievals. - Setting Values: Performance is relatively close between the two, with minor variations.
The choice between InMemoryTreeMultiCache and InMemoryHashMapMultiCache would depend on the specific use case, especially the depth of the keys and the frequency of retrieval operations. TTL parameter in cache doesn't automatically clean up all the entries. Use ManagedMultiCache wrapper to automatically manage outdated keys.
func NewDefaultHashMapMultiCache ¶
func NewDefaultHashMapMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, uint64]
NewDefaultHashMapMultiCache creates a new instance of the InMemoryHashMapMultiCache using SHA256 as the hashing algorithm.
func NewFarmHashMapMultiCache ¶
func NewFarmHashMapMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, uint64]
func NewInMemoryHashMapMultiCache ¶
func NewInMemoryHashMapMultiCache[K CompositeKey, T any, H comparable](toHash func(keys []uconst.Unique) H, ttl uopt.Opt[time.Duration]) *InMemoryHashMapMultiCache[K, T, H]
NewInMemoryHashMapMultiCache creates a new instance of the InMemoryHashMapMultiCache. It takes a hashing function to translate the composite keys to a desired hash type, and an optional time-to-live duration for the cache entries.
func (*InMemoryHashMapMultiCache[K, T, H]) Changes ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Changes() []K
Changes returns a list of keys that have experienced changes in the cache since the last reset.
func (*InMemoryHashMapMultiCache[K, T, H]) Drop ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Drop()
Drop completely clears the cache, removing all entries. The operation is thread-safe.
func (*InMemoryHashMapMultiCache[K, T, H]) DropKey ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) DropKey(key K)
DropKey removes the values associated with the provided key from the cache. The operation is thread-safe.
func (*InMemoryHashMapMultiCache[K, T, H]) Get ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Get(key K) []T
Get retrieves the values associated with the provided key from the cache. The operation is thread-safe and does not alter the change history.
func (*InMemoryHashMapMultiCache[K, T, H]) Keys ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Keys() []K
func (*InMemoryHashMapMultiCache[K, T, H]) Outdated ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Outdated(key uopt.Opt[K]) bool
Outdated checks if the provided key or the entire cache (if no key is provided) is outdated based on the set TTL. Returns true if outdated, false otherwise.
func (*InMemoryHashMapMultiCache[K, T, H]) Put ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Put(key K, values ...T)
Put adds the given values to the cache associated with the provided key. If the key already exists, the values are updated. The insertion is thread-safe.
func (*InMemoryHashMapMultiCache[K, T, H]) PutQuietly ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) PutQuietly(key K, values ...T)
PutQuietly adds values to the cache for the provided key but does so without altering the change history. This operation can be used when modifications should not trigger cache change diff.
func (*InMemoryHashMapMultiCache[K, T, H]) RemoveOutdated ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) RemoveOutdated() int
func (*InMemoryHashMapMultiCache[K, T, H]) Set ¶
func (c *InMemoryHashMapMultiCache[K, T, H]) Set(key K, values ...T)
Set updates the cache values for the provided key. If the key already exists, its previous values are removed before adding the new values. The operation is thread-safe.
type InMemoryTreeMultiCache ¶
type InMemoryTreeMultiCache[K CompositeKey, T uconst.Comparable] struct { // contains filtered or unexported fields }
InMemoryTreeMultiCache provides an in-memory caching mechanism with support for compound keys. The cache leverages tree-like structures to store and organize data, allowing efficient operations even with composite keys. The cache supports optional TTL (time-to-live) for entries, ensuring that outdated entries can be identified and potentially purged. Concurrency-safe operations are ensured through the use of a mutex.
Benchmark insights: - Put operation performance is fast for shallow depth keys but slows down as the depth increases. - Get operation is particularly efficient, especially for shallow depth keys. - Set operation's performance is consistent regardless of the depth of the key. TTL parameter in cache doesn't automatically clean up all the entries. Use ManagedMultiCache wrapper to automatically manage outdated keys.
func (*InMemoryTreeMultiCache[K, T]) Changes ¶
func (c *InMemoryTreeMultiCache[K, T]) Changes() []K
Changes returns a slice of keys that have been modified in the cache. This method provides a way to track changes made to the cache, useful for scenarios like cache syncing.
func (*InMemoryTreeMultiCache[K, T]) Drop ¶
func (c *InMemoryTreeMultiCache[K, T]) Drop()
Drop removes all entries from the cache. This is a complete reset of the cache, useful when you want to clear the cache and start fresh.
func (*InMemoryTreeMultiCache[K, T]) DropKey ¶
func (c *InMemoryTreeMultiCache[K, T]) DropKey(key K)
DropKey removes the value(s) associated with the given key from the cache.
func (*InMemoryTreeMultiCache[K, T]) Get ¶
func (c *InMemoryTreeMultiCache[K, T]) Get(key K) []T
Get retrieves the value(s) associated with the given key from the cache. If the key is not found, it returns an empty slice. Retrieval is fast, especially for shallow depth keys.
func (*InMemoryTreeMultiCache[K, T]) Keys ¶
func (c *InMemoryTreeMultiCache[K, T]) Keys() []K
func (*InMemoryTreeMultiCache[K, T]) Outdated ¶
func (c *InMemoryTreeMultiCache[K, T]) Outdated(key uopt.Opt[K]) bool
Outdated checks if a given key or the entire cache is outdated based on the TTL. If no key is provided or key was not found, it checks the last updated time of the entire cache. If a key is provided and found, it checks the last updated time of that specific key.
func (*InMemoryTreeMultiCache[K, T]) Put ¶
func (c *InMemoryTreeMultiCache[K, T]) Put(key K, val ...T)
Put inserts a new value(s) into the cache associated with the given key. If the key already exists in the cache, it appends the new value(s) to the existing values. This operation is relatively fast for shallow depth keys, but becomes slower as the depth increases.
func (*InMemoryTreeMultiCache[K, T]) PutQuietly ¶
func (c *InMemoryTreeMultiCache[K, T]) PutQuietly(key K, val ...T)
PutQuietly behaves like the Put method but does not update the cache state or add any changes to the cache, making it much faster alternative to Put and Set. This method is useful when you want to add values to the cache without triggering any side effects.
func (*InMemoryTreeMultiCache[K, T]) Set ¶
func (c *InMemoryTreeMultiCache[K, T]) Set(key K, val ...T)
Set inserts a new value(s) into the cache associated with the given key. If the key already exists in the cache, this method will overwrite the existing values.
type Int64Value ¶
type Int64Value struct {
// contains filtered or unexported fields
}
func NewInt64Value ¶
func NewInt64Value(v int64) Int64Value
func (Int64Value) Equals ¶
func (s Int64Value) Equals(other uconst.Comparable) bool
func (Int64Value) Value ¶
func (s Int64Value) Value() string
type IntCompositeKey ¶
type IntCompositeKey struct {
// contains filtered or unexported fields
}
func NewIntCompositeKey ¶
func NewIntCompositeKey(keys ...int64) IntCompositeKey
func (IntCompositeKey) Equals ¶
func (k IntCompositeKey) Equals(other uconst.Comparable) bool
func (IntCompositeKey) Keys ¶
func (k IntCompositeKey) Keys() []uconst.Unique
func (IntCompositeKey) String ¶
func (k IntCompositeKey) String() string
type ManagedCache ¶
ManagedCache provides a wrapper around a Cache implementation to manage periodic cleanup of outdated cache entries. It uses a background goroutine to perform cleanup tasks based on the provided TTL (time-to-live) value. The Stop method must be called to clean up resources if you want to stop managing the cache.
func NewManagedCache ¶
func NewManagedCacheWithOptions ¶
func NewManagedCacheWithOptions[K any, T any](cache BaseCache[K, T], options ManagedCacheOptions) *ManagedCache[K, T]
func (*ManagedCache[K, T]) Changes ¶
func (b *ManagedCache[K, T]) Changes() []K
func (*ManagedCache[K, T]) Drop ¶
func (b *ManagedCache[K, T]) Drop()
func (*ManagedCache[K, T]) DropKey ¶
func (b *ManagedCache[K, T]) DropKey(key K)
func (*ManagedCache[K, T]) ForceCleanup ¶
func (b *ManagedCache[K, T]) ForceCleanup()
func (*ManagedCache[K, T]) Get ¶
func (b *ManagedCache[K, T]) Get(key K) (*T, bool)
func (*ManagedCache[K, T]) GetValue ¶
func (b *ManagedCache[K, T]) GetValue(key K) (T, bool)
func (*ManagedCache[K, T]) Keys ¶
func (b *ManagedCache[K, T]) Keys() []K
func (*ManagedCache[K, T]) Set ¶
func (b *ManagedCache[K, T]) Set(key K, value T)
func (*ManagedCache[K, T]) SetQuietly ¶
func (b *ManagedCache[K, T]) SetQuietly(key K, value T)
func (*ManagedCache[K, T]) Stop ¶
func (b *ManagedCache[K, T]) Stop()
type ManagedCacheOptions ¶
type ManagedMultiCache ¶
type ManagedMultiCache[K CompositeKey, T uconst.Comparable] struct { // contains filtered or unexported fields }
ManagedMultiCache provides a wrapper around a MultiCache implementation to manage periodic cleanup of outdated cache entries. It uses a background goroutine to perform cleanup tasks based on the provided TTL (time-to-live) value. The Stop method must be called to clean up resources if you want to stop managing the cache.
func NewManagedMultiCache ¶
func NewManagedMultiCache[K CompositeKey, T uconst.Comparable](cache MultiCache[K, T], tick time.Duration) *ManagedMultiCache[K, T]
func NewManagedMultiCacheWithOptions ¶
func NewManagedMultiCacheWithOptions[K CompositeKey, T uconst.Comparable](cache MultiCache[K, T], options ManagedCacheOptions) *ManagedMultiCache[K, T]
func (*ManagedMultiCache[K, T]) Changes ¶
func (b *ManagedMultiCache[K, T]) Changes() []K
func (*ManagedMultiCache[K, T]) Drop ¶
func (b *ManagedMultiCache[K, T]) Drop()
func (*ManagedMultiCache[K, T]) DropKey ¶
func (b *ManagedMultiCache[K, T]) DropKey(key K)
func (*ManagedMultiCache[K, T]) ForceCleanup ¶
func (b *ManagedMultiCache[K, T]) ForceCleanup()
func (*ManagedMultiCache[K, T]) Get ¶
func (b *ManagedMultiCache[K, T]) Get(key K) []T
func (*ManagedMultiCache[K, T]) Outdated ¶
func (b *ManagedMultiCache[K, T]) Outdated(key uopt.Opt[K]) bool
func (*ManagedMultiCache[K, T]) Put ¶
func (b *ManagedMultiCache[K, T]) Put(key K, values ...T)
func (*ManagedMultiCache[K, T]) PutQuietly ¶
func (b *ManagedMultiCache[K, T]) PutQuietly(key K, values ...T)
func (*ManagedMultiCache[K, T]) Set ¶
func (b *ManagedMultiCache[K, T]) Set(key K, values ...T)
func (*ManagedMultiCache[K, T]) Stop ¶
func (b *ManagedMultiCache[K, T]) Stop()
type MultiCache ¶
type MultiCache[K CompositeKey, T any] interface { // Put inserts a new value(s) into the cache associated with the given key. // If the key already exists in the cache, it appends the new value(s) to the existing values. // This operation is relatively fast for shallow depth keys, but becomes slower as the depth increases. Put(key K, values ...T) // Set inserts a new value(s) into the cache associated with the given key. // If the key already exists in the cache, this method will overwrite the existing values. Set(key K, values ...T) // Get retrieves the value(s) associated with the given key from the cache. // If the key is not found, it returns an empty slice. // Retrieval is fast, especially for shallow depth keys. // Supports retrieving a value using a broader key (e.g., [1, 2]) or a full/shallow key (e.g., [1, 2, 3, 4]) Get(key K) []T // Changes returns a slice of keys that have been modified in the cache. // This method provides a way to track changes made to the cache, useful for scenarios like cache syncing. // Cache changes will be updated only on modifying operations, meaning that in-fact, changes contain all the present keys. Changes() []K // Keys returns a snapshot of all currently stored keys. Keys() []K // Drop removes all entries from the cache. // This is a complete reset of the cache, useful when you want to clear the cache and start fresh. Drop() // DropKey removes the value(s) associated with the given key from the cache. DropKey(key K) // Outdated checks if a given key or the entire cache is outdated based on the TTL. // If no key is provided it checks the last updated time of the entire cache. // If a key is provided and found, it checks the last updated time of that specific key. // If key was not found returns false. Outdated(key uopt.Opt[K]) bool // PutQuietly behaves like the Put method but does not update the cache state or add any changes to the cache, making it // much faster alternative to Put and Set. // This method is useful when you want to add values to the cache without triggering any side effects. PutQuietly(key K, values ...T) }
The MultiCache interface defines a set of methods for a generic cache implementation. This interface supports setting, getting, and managing cache entries with composite keys. Unlike Cache, it is designed to handle multiple values per key and has a hierarchical key handling.
Example: - Set([1, 2, 3], "Value1") => [1, 2, 3] is set with "Value1". - Set([1, 2, 3, 4], "Value2") => [1, 2, 3] is replaced, and [1, 2, 3, 4] is set with "Value2". - Get([1, 2, 3]) => returns nil, as it has been replaced by [1, 2, 3, 4].
This hierarchical key handling is useful for scenarios where more specific keys should override the values of their parent keys, providing a clear and structured way to manage cache entries.
func NewInMemoryTreeMultiCache ¶
func NewInMemoryTreeMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) MultiCache[K, T]
NewInMemoryTreeMultiCache creates a new instance of the InMemoryTreeMultiCache. It takes an optional TTL (time-to-live) parameter to set expiration time for cache entries. If the TTL is not provided, cache entries will not expire. Note on hierarchical key handling:
- If a composite key (e.g., [1, 2, 3]) is already set, any broader keys that share the same prefix (e.g., [1, 2]) are considered "busy" as part of the hierarchy.
- Setting a more specific key (e.g., [1, 2, 3, 4]) will replace the broader key's value (e.g., [1, 2, 3]).
- This design ensures that more specific keys take precedence and can replace the values of their parent keys.
- Additionally, retrieving a value using a broader key (e.g., [1, 2]) will return the values of the most specific key that shares the prefix (e.g., [1, 2, 3, 4]).
func NewSha256HashMapMultiCache ¶
func NewSha256HashMapMultiCache[K CompositeKey, T uconst.Comparable](ttl uopt.Opt[time.Duration]) MultiCache[K, T]
type StrCompositeKey ¶
type StrCompositeKey struct {
// contains filtered or unexported fields
}
func NewStrCompositeKey ¶
func NewStrCompositeKey(keys ...string) StrCompositeKey
func (StrCompositeKey) Equals ¶
func (k StrCompositeKey) Equals(other uconst.Comparable) bool
func (StrCompositeKey) Keys ¶
func (k StrCompositeKey) Keys() []uconst.Unique
func (StrCompositeKey) String ¶
func (k StrCompositeKey) String() string
type StringSliceValue ¶
type StringSliceValue struct {
// contains filtered or unexported fields
}
func NewStringSliceValue ¶
func NewStringSliceValue(v []string) StringSliceValue
func (StringSliceValue) Equals ¶
func (s StringSliceValue) Equals(other uconst.Comparable) bool
func (StringSliceValue) Values ¶
func (s StringSliceValue) Values() []string
type StringValue ¶
type StringValue struct {
// contains filtered or unexported fields
}
func NewStringValue ¶
func NewStringValue(v string) StringValue
func (StringValue) Equals ¶
func (s StringValue) Equals(other uconst.Comparable) bool
func (StringValue) Value ¶
func (s StringValue) Value() string
type UIntCompositeKey ¶
type UIntCompositeKey struct {
// contains filtered or unexported fields
}
func NewUIntCompositeKey ¶
func NewUIntCompositeKey(keys ...uint64) UIntCompositeKey
func (UIntCompositeKey) Equals ¶
func (k UIntCompositeKey) Equals(other uconst.Comparable) bool
func (UIntCompositeKey) Keys ¶
func (k UIntCompositeKey) Keys() []int64
func (UIntCompositeKey) String ¶
func (k UIntCompositeKey) String() string