cache := src.NewFastCache()Creates a basic FastCache instance with default settings.
cache, err := src.NewRistrettoCache(config *Config) (*RistrettoCache, error)Creates a Ristretto cache instance with custom configuration.
Config Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
| MaxCost | int64 | 1GB | Maximum memory cost |
| BufferSize | int | 512MB | Write buffer size |
| ShardCount | int | 8 | Number of shards |
| TTL | time.Duration | 0 | Default TTL |
| MetricsEnabled | bool | false | Enable metrics |
cache.Set(key string, value interface{}, cost int64) boolSets a value in the cache.
Parameters:
- key: Cache key
- value: Value to store
- cost: Memory cost of this item
Returns: true if successfully set
val := cache.Get(key string) (interface{}, bool)Gets a value from the cache.
Returns: (value, found)
cache.SetM2One(keys []string, value interface{}, cost int64) boolMaps multiple keys to a single value. All keys will return the same value.
cache.SetM(items []CacheItem)Batch set multiple items.
cache.Del(key string) boolDeletes a key from the cache.
cache.Wait()Waits for all pending writes to complete.
cost := cache.Cost() int64Returns the total memory cost of all items in the cache.
cache.Clear()Clears all items from the cache.
store, err := src.NewVectorStore(config *VectorStoreConfig) (*VectorCache, error)Creates a vector store instance.
Config Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
| IndexType | string | "flat" | Index type: "flat" or "hnsw" |
| Metric | MetricType | MetricL2 | Distance metric |
| MaxCost | int64 | 1GB | Maximum memory cost |
| ShardCount | int | 1 | Number of shards |
| TTL | time.Duration | 0 | Vector TTL |
| HNSW | HNSWConfig | default | HNSW configuration |
err := store.Add(id string, vector Vector, metadata map[string]any) errorAdds a vector to the store.
Parameters:
- id: Unique identifier
- vector: Float32 vector
- metadata: Optional metadata
item, found := store.Get(id string) (*VectorItem, bool)Retrieves a vector by ID.
err := store.Delete(id string) errorDeletes a vector by ID.
results, err := store.Search(query Vector, k int) ([]SearchResult, error)Searches for k nearest vectors.
Returns: Array of SearchResult sorted by distance
results, err := store.SearchWithFilter(query Vector, k int, filter FilterFunc) ([]SearchResult, error)Searches with metadata filtering.
FilterFunc:
type FilterFunc func(metadata map[string]any) boolerr := store.BatchAdd(items []VectorItem) errorBatch add multiple vectors.
result := store.BatchGet(ids []string) map[string]*VectorItemBatch get multiple vectors.
count := store.BatchDelete(ids []string) intBatch delete vectors.
data, err := store.ExportToBytes() ([]byte, error)Exports vectors to JSON bytes.
err := store.ImportFromBytes(data []byte) errorImports vectors from JSON bytes.
count := store.Len() intReturns the number of vectors.
cost := store.Cost() int64Returns current memory cost.
store.Close() errorCloses the vector store.
type Vector []float32Represents a vector as a slice of float32 values.
type VectorItem struct {
ID string
Vector Vector
Metadata map[string]any
Cost int64
}Represents a stored vector with metadata.
type SearchResult struct {
ID string
Vector Vector
Score float32
Metadata map[string]any
}Represents a search result.
type MetricType string
const (
MetricL2 MetricType = "l2" // Euclidean distance
MetricCosine MetricType = "cosine" // Cosine similarity
MetricIP MetricType = "ip" // Inner product
)dist := src.L2Distance(v1, v2 Vector) float32Calculates Euclidean distance.
dist := src.CosineDistance(v1, v2 Vector) float32Calculates cosine distance (1 - similarity).
dist := src.IPDistance(v1, v2 Vector) float32Calculates inner product (returns negative for sorting).
type HNSWConfig struct {
M int // Number of connections per node
EFConstruction int // Candidate list size during construction
EFSearch int // Candidate list size during search
LevelMult float64 // Level multiplier factor
}Default Configuration:
func DefaultHNSWConfig() HNSWConfig {
return HNSWConfig{
M: 16,
EFConstruction: 200,
EFSearch: 50,
LevelMult: 1 / math.Ln2,
}
}