Documentation
ΒΆ
Overview ΒΆ
Package fastcache provides a high-performance, goroutine-safe in-memory key-value cache designed to handle 1M+ QPS with automatic memory management and LRU eviction.
Index ΒΆ
- Variables
- func IsPermanentError(err error) bool
- func IsTemporaryError(err error) bool
- type Cache
- func (c *Cache) Clear()
- func (c *Cache) Close() error
- func (c *Cache) Delete(key string) bool
- func (c *Cache) Get(key string) (interface{}, bool)
- func (c *Cache) GetMemoryInfo() *MemoryInfo
- func (c *Cache) GetPerformanceMetrics() *PerformanceMetrics
- func (c *Cache) GetShardStats() []ShardStats
- func (c *Cache) GetStats() *Stats
- func (c *Cache) ResetStats()
- func (c *Cache) Set(key string, value interface{}, ttl ...time.Duration) error
- type Config
- type Entry
- type ErrInvalidConfig
- type ErrOperationFailed
- type ErrShardError
- type MemoryInfo
- type PerformanceMetrics
- type Shard
- type ShardStats
- type Stats
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // ErrCacheClosed is returned when operations are attempted on a closed cache ErrCacheClosed = errors.New("cache is closed") // ErrKeyNotFound is returned when a key is not found in the cache ErrKeyNotFound = errors.New("key not found") // ErrInvalidKey is returned when an invalid key is provided ErrInvalidKey = errors.New("invalid key") // ErrMemoryLimitExceeded is returned when memory limit would be exceeded ErrMemoryLimitExceeded = errors.New("memory limit exceeded") )
Common errors
Functions ΒΆ
func IsPermanentError ΒΆ
IsPermanentError checks if an error is permanent and the operation should not be retried
func IsTemporaryError ΒΆ
IsTemporaryError checks if an error is temporary and the operation can be retried
Types ΒΆ
type Cache ΒΆ
type Cache struct {
// contains filtered or unexported fields
}
Cache is the main cache structure
func (*Cache) GetMemoryInfo ΒΆ
func (c *Cache) GetMemoryInfo() *MemoryInfo
GetMemoryInfo returns detailed memory usage information
func (*Cache) GetPerformanceMetrics ΒΆ
func (c *Cache) GetPerformanceMetrics() *PerformanceMetrics
GetPerformanceMetrics returns performance metrics
func (*Cache) GetShardStats ΒΆ
func (c *Cache) GetShardStats() []ShardStats
GetShardStats returns statistics for all shards
type Config ΒΆ
type Config struct {
// MaxMemoryBytes is the maximum memory usage before eviction starts (e.g., 512MB)
MaxMemoryBytes int64
// ShardCount is the number of shards for concurrent access
// Higher values reduce lock contention but increase memory overhead
ShardCount int
// DefaultTTL is the default time-to-live for entries
// Set to 0 for no expiration
DefaultTTL time.Duration
// CleanupInterval determines how often expired entries are cleaned up
CleanupInterval time.Duration
}
Config holds configuration for the cache
func CustomConfig ΒΆ
CustomConfig creates a configuration with custom parameters
func DefaultConfig ΒΆ
func DefaultConfig() *Config
DefaultConfig returns a default configuration optimized for 1M QPS
func HighConcurrencyConfig ΒΆ
func HighConcurrencyConfig() *Config
HighConcurrencyConfig returns a configuration optimized for very high concurrency
func LowMemoryConfig ΒΆ
func LowMemoryConfig() *Config
LowMemoryConfig returns a configuration for memory-constrained environments
type Entry ΒΆ
type Entry struct {
// contains filtered or unexported fields
}
Entry represents a single cache entry
type ErrInvalidConfig ΒΆ
ErrInvalidConfig represents a configuration validation error
func (ErrInvalidConfig) Error ΒΆ
func (e ErrInvalidConfig) Error() string
type ErrOperationFailed ΒΆ
ErrOperationFailed represents an operation failure
func (ErrOperationFailed) Error ΒΆ
func (e ErrOperationFailed) Error() string
type ErrShardError ΒΆ
ErrShardError represents a shard-specific error
func (ErrShardError) Error ΒΆ
func (e ErrShardError) Error() string
func (ErrShardError) Unwrap ΒΆ
func (e ErrShardError) Unwrap() error
type MemoryInfo ΒΆ
type MemoryInfo struct {
Used int64 `json:"used"`
UsedFormatted string `json:"used_formatted"`
Max int64 `json:"max"`
MaxFormatted string `json:"max_formatted"`
Available int64 `json:"available"`
AvailableFormatted string `json:"available_formatted"`
Percent float64 `json:"percent"`
ShardSizes []int64 `json:"shard_sizes"`
}
MemoryInfo provides detailed memory information
type PerformanceMetrics ΒΆ
type PerformanceMetrics struct {
TotalOperations int64 `json:"total_operations"`
HitRate float64 `json:"hit_rate"`
MissRate float64 `json:"miss_rate"`
AvgShardLoad float64 `json:"avg_shard_load"`
MaxShardLoad int `json:"max_shard_load"`
MinShardLoad int `json:"min_shard_load"`
LoadBalance float64 `json:"load_balance"` // Standard deviation of shard loads
}
PerformanceMetrics provides performance-related metrics
type Shard ΒΆ
type Shard struct {
// contains filtered or unexported fields
}
Shard represents a single shard of the cache
type ShardStats ΒΆ
type ShardStats struct {
ShardID int `json:"shard_id"`
EntryCount int `json:"entry_count"`
Size int64 `json:"size"`
HitCount int64 `json:"hit_count"`
MissCount int64 `json:"miss_count"`
HitRatio float64 `json:"hit_ratio"`
MemoryUsage string `json:"memory_usage"`
}
ShardStats represents statistics for a single shard
type Stats ΒΆ
type Stats struct {
TotalSize int64 `json:"total_size"`
TotalEntries int64 `json:"total_entries"`
HitCount int64 `json:"hit_count"`
MissCount int64 `json:"miss_count"`
HitRatio float64 `json:"hit_ratio"`
MemoryUsage string `json:"memory_usage"`
ShardCount int `json:"shard_count"`
MaxMemory int64 `json:"max_memory"`
MemoryPercent float64 `json:"memory_percent"`
}
Stats represents cache statistics
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
api-server
command
|
|
|
basic
command
|
|
|
high-concurrency
command
|
|
|
monitoring
command
|
|
|
tools
|
|
|
load-tester
command
|