Documentation
¶
Index ¶
- func GetMetadata(ctx context.Context, key string) (any, bool)
- func GetNamespace(ctx context.Context) string
- func WithMetadata(ctx context.Context, key string, value any) context.Context
- func WithNamespace(ctx context.Context, namespace string) context.Context
- type Cache
- func (c *Cache[K, V]) Delete(ctx context.Context, key K) bool
- func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool)
- func (c *Cache[K, V]) GetSimilar(ctx context.Context, key K) (V, K, float64, bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(ctx context.Context, key K, value V) error
- func (c *Cache[K, V]) Stats() Stats
- func (c *Cache[K, V]) WithSimilarity(fn SimilarityFunc[K]) *Cache[K, V]
- type Entry
- type EvictionPolicy
- type Option
- type Options
- type Shard
- type Similarity
- type SimilarityFunc
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetMetadata ¶
GetMetadata retrieves a metadata value from the context
func GetNamespace ¶
GetNamespace retrieves the namespace from the context
func WithMetadata ¶
WithMetadata adds metadata to the context
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a generic similarity-based cache with sharding
func New ¶
func New[K comparable, V any](opts ...Option) *Cache[K, V]
New creates a new cache with the given options
func (*Cache[K, V]) GetSimilar ¶
GetSimilar finds the most similar key above the threshold
func (*Cache[K, V]) Stats ¶ added in v0.3.0
Stats returns aggregated statistics from all shards Returns zero values if stats are not enabled
func (*Cache[K, V]) WithSimilarity ¶
func (c *Cache[K, V]) WithSimilarity(fn SimilarityFunc[K]) *Cache[K, V]
WithSimilarity sets the similarity function for the cache
type Entry ¶
type Entry[K comparable, V any] struct { Key K Value V CreatedAt time.Time AccessedAt time.Time AccessCount uint64 ExpiresAt time.Time Metadata map[string]any Namespace string }
Entry represents a cache entry with metadata
type EvictionPolicy ¶
type EvictionPolicy = eviction.EvictionPolicy
EvictionPolicy is re-exported from the eviction package
type Option ¶
type Option func(*Options)
Option is a function that modifies Options
func WithEviction ¶
func WithEviction(policy EvictionPolicy) Option
WithEviction sets the eviction policy
func WithThreshold ¶
WithThreshold sets the similarity threshold
type Options ¶
type Options struct {
NumShards int
MaxSize int
SimilarityThreshold float64
EvictionPolicy EvictionPolicy
TTL time.Duration
EnableStats bool
}
Options contains configuration options for the cache
type Shard ¶
type Shard[K comparable, V any] struct { // contains filtered or unexported fields }
Shard represents a single shard of the cache
type Similarity ¶
type Similarity[K comparable] interface { // Score computes the similarity score between two keys // Returns a value between 0.0 and 1.0 Score(a, b K) float64 // Threshold returns the minimum similarity score for a match Threshold() float64 }
Similarity is an interface for similarity computation
func NewSimilarity ¶
func NewSimilarity[K comparable](fn SimilarityFunc[K], threshold float64) Similarity[K]
NewSimilarity creates a Similarity from a SimilarityFunc
type SimilarityFunc ¶
type SimilarityFunc[K comparable] func(a, b K) float64
SimilarityFunc is a function type that computes similarity between two keys It should return a score between 0.0 (completely different) and 1.0 (identical)