Documentation
¶
Index ¶
- type Cache
- type Comparable
- type ComparableKey
- type ComparableSlice
- type CompositeKey
- type GenKey
- type GenericCompositeKey
- type Hashed
- type InMemoryHashMapCache
- func NewDefaultHashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, uint64]
- func NewFarmHashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, uint64]
- func NewInMemoryHashMapCache[K CompositeKey, T Comparable, H comparable](toHash func(keys []int) H, ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, H]
- func NewSha256HashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, string]
- func (c *InMemoryHashMapCache[K, T, H]) AddSilently(key K, values ...T)
- func (c *InMemoryHashMapCache[K, T, H]) Changes() []K
- func (c *InMemoryHashMapCache[K, T, H]) Drop()
- func (c *InMemoryHashMapCache[K, T, H]) DropKey(key K)
- func (c *InMemoryHashMapCache[K, T, H]) Get(key K) []T
- func (c *InMemoryHashMapCache[K, T, H]) Outdated(key opt.Opt[K]) bool
- func (c *InMemoryHashMapCache[K, T, H]) Put(key K, values ...T)
- func (c *InMemoryHashMapCache[K, T, H]) Set(key K, values ...T)
- type InMemoryTreeCache
- func (c *InMemoryTreeCache[K, T]) AddSilently(key K, val ...T)
- func (c *InMemoryTreeCache[K, T]) Changes() []K
- func (c *InMemoryTreeCache[K, T]) Drop()
- func (c *InMemoryTreeCache[K, T]) DropKey(key K)
- func (c *InMemoryTreeCache[K, T]) Get(key K) []T
- func (c *InMemoryTreeCache[K, T]) Outdated(key opt.Opt[K]) bool
- func (c *InMemoryTreeCache[K, T]) Put(key K, val ...T)
- func (c *InMemoryTreeCache[K, T]) Set(key K, val ...T)
- type Int64Value
- type IntCompositeKey
- type IntKey
- 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 Cache ¶
type Cache[K CompositeKey, T Comparable] interface { Put(key K, values ...T) // Adds a new value to the key Set(key K, values ...T) // Overwrites key values Get(key K) []T AddSilently(key K, values ...T) // Adds the values but update cache state/doesn't add any changes to the cache Changes() []K Drop() DropKey(key K) Outdated(key opt.Opt[K]) bool }
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]) Hash ¶
func (k ComparableKey[T]) Hash() int
func (ComparableKey[T]) String ¶
func (k ComparableKey[T]) String() string
type ComparableSlice ¶
type ComparableSlice[T Comparable] struct { Data []T }
func (ComparableSlice[T]) Equals ¶
func (c ComparableSlice[T]) Equals(other Comparable) bool
type CompositeKey ¶
type CompositeKey interface {
Comparable
Keys() []int // Keys returns an ordered list of keys ordered by priority (ASC), so the first element has the most prio.
String() string
}
CompositeKey specifies an abstract key with an ability to provide an ordered list of available keys.
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 Comparable) bool
func (GenericCompositeKey) Keys ¶
func (k GenericCompositeKey) Keys() []int
func (GenericCompositeKey) String ¶
func (k GenericCompositeKey) String() string
type Hashed ¶
type Hashed interface {
Comparable
Key() int // Key should return a unique item key for hash use.
String() string
}
Hashed specifies an abstract key with an ability to provide keys for hash method. Shouldn't implement hashing at all.
type InMemoryHashMapCache ¶
type InMemoryHashMapCache[K CompositeKey, T Comparable, H comparable] struct { // contains filtered or unexported fields }
InMemoryHashMapCache provides an in-memory caching mechanism using hashmaps. 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. Concurrency-safe operations are ensured through the use of a mutex.
Performance Comparison with InMemoryTreeCache: - Insertions: InMemoryTreeCache is slightly faster for single-depth insertions and significantly faster for deeper depths. - Retrievals: InMemoryHashMapCache (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 InMemoryTreeCache and InMemoryHashMapCache would depend on the specific use case, especially the depth of the keys and the frequency of retrieval operations.
func NewDefaultHashMapCache ¶
func NewDefaultHashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, uint64]
NewDefaultHashMapCache creates a new instance of the InMemoryHashMapCache using SHA256 as the hashing algorithm.
func NewFarmHashMapCache ¶
func NewFarmHashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, uint64]
func NewInMemoryHashMapCache ¶
func NewInMemoryHashMapCache[K CompositeKey, T Comparable, H comparable](toHash func(keys []int) H, ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, H]
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 NewSha256HashMapCache ¶
func NewSha256HashMapCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryHashMapCache[K, T, string]
func (*InMemoryHashMapCache[K, T, H]) AddSilently ¶
func (c *InMemoryHashMapCache[K, T, H]) AddSilently(key K, values ...T)
AddSilently 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 logs.
func (*InMemoryHashMapCache[K, T, H]) Changes ¶
func (c *InMemoryHashMapCache[K, T, H]) Changes() []K
Changes returns a list of keys that have experienced changes in the cache since the last reset.
func (*InMemoryHashMapCache[K, T, H]) Drop ¶
func (c *InMemoryHashMapCache[K, T, H]) Drop()
Drop completely clears the cache, removing all entries. The operation is thread-safe.
func (*InMemoryHashMapCache[K, T, H]) DropKey ¶
func (c *InMemoryHashMapCache[K, T, H]) DropKey(key K)
DropKey removes the values associated with the provided key from the cache. The operation is thread-safe.
func (*InMemoryHashMapCache[K, T, H]) Get ¶
func (c *InMemoryHashMapCache[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 (*InMemoryHashMapCache[K, T, H]) Outdated ¶
func (c *InMemoryHashMapCache[K, T, H]) Outdated(key opt.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, H]) Put ¶
func (c *InMemoryHashMapCache[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 (*InMemoryHashMapCache[K, T, H]) Set ¶
func (c *InMemoryHashMapCache[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 InMemoryTreeCache ¶
type InMemoryTreeCache[K CompositeKey, T Comparable] struct { // contains filtered or unexported fields }
InMemoryTreeCache 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.
func NewInMemoryTreeCache ¶
func NewInMemoryTreeCache[K CompositeKey, T Comparable](ttl opt.Opt[time.Duration]) *InMemoryTreeCache[K, T]
NewInMemoryTreeCache creates a new instance of the InMemoryTreeCache. 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.
func (*InMemoryTreeCache[K, T]) AddSilently ¶
func (c *InMemoryTreeCache[K, T]) AddSilently(key K, val ...T)
AddSilently behaves like the Put method but does not update the cache state or add any changes to the cache. This method is useful when you want to add values to the cache without triggering any side effects.
func (*InMemoryTreeCache[K, T]) Changes ¶
func (c *InMemoryTreeCache[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 (*InMemoryTreeCache[K, T]) Drop ¶
func (c *InMemoryTreeCache[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 (*InMemoryTreeCache[K, T]) DropKey ¶
func (c *InMemoryTreeCache[K, T]) DropKey(key K)
DropKey removes the value(s) associated with the given key from the cache.
func (*InMemoryTreeCache[K, T]) Get ¶
func (c *InMemoryTreeCache[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 (*InMemoryTreeCache[K, T]) Outdated ¶
func (c *InMemoryTreeCache[K, T]) Outdated(key opt.Opt[K]) bool
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, it checks the last updated time of that specific key.
func (*InMemoryTreeCache[K, T]) Put ¶
func (c *InMemoryTreeCache[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 (*InMemoryTreeCache[K, T]) Set ¶
func (c *InMemoryTreeCache[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 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 Comparable) bool
func (IntCompositeKey) Keys ¶
func (k IntCompositeKey) Keys() []int
func (IntCompositeKey) String ¶
func (k IntCompositeKey) String() string
type IntKey ¶
type IntKey int64
func (IntKey) Equals ¶
func (s IntKey) Equals(other Comparable) bool
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 Comparable) bool
func (StrCompositeKey) Keys ¶
func (k StrCompositeKey) Keys() []int
func (StrCompositeKey) String ¶
func (k StrCompositeKey) String() string
type StringKey ¶
type StringKey string
func (StringKey) Equals ¶
func (s StringKey) Equals(other Comparable) bool
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 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 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 Comparable) bool
func (UIntCompositeKey) Keys ¶
func (k UIntCompositeKey) Keys() []int
func (UIntCompositeKey) String ¶
func (k UIntCompositeKey) String() string