Documentation
¶
Overview ¶
Package cache provides caching implementations using ecache.
Package cache provides multi-level caching with LRU and LFU eviction policies.
Index ¶
- Variables
- type Cache
- type CacheManager
- type CacheStats
- type Config
- type DiskCache
- func (c *DiskCache) Clear(ctx context.Context) error
- func (c *DiskCache) Close() error
- func (c *DiskCache) Delete(ctx context.Context, key string) error
- func (c *DiskCache) DiskSize() int64
- func (c *DiskCache) Exists(ctx context.Context, key string) bool
- func (c *DiskCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *DiskCache) Len() int
- func (c *DiskCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *DiskCache) Stats() CacheStats
- type ECache
- func (c *ECache) Clear(ctx context.Context) error
- func (c *ECache) Close() error
- func (c *ECache) Delete(ctx context.Context, key string) error
- func (c *ECache) Exists(ctx context.Context, key string) bool
- func (c *ECache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *ECache) GetOrSet(ctx context.Context, key string, fn func() (interface{}, error)) (interface{}, error)
- func (c *ECache) Len() int
- func (c *ECache) Set(ctx context.Context, key string, value interface{}) error
- func (c *ECache) SetNX(ctx context.Context, key string, value interface{}) bool
- func (c *ECache) Stats() CacheStats
- func (c *ECache) Touch(ctx context.Context, key string) bool
- type ECache2
- func (c *ECache2) Clear(ctx context.Context) error
- func (c *ECache2) Close() error
- func (c *ECache2) Delete(ctx context.Context, key string) error
- func (c *ECache2) Exists(ctx context.Context, key string) bool
- func (c *ECache2) Get(ctx context.Context, key string) (interface{}, error)
- func (c *ECache2) Set(ctx context.Context, key string, value interface{}) error
- func (c *ECache2) Stats() CacheStats
- type Entry
- type EvictionPolicy
- type GenericCache
- func (c *GenericCache[K]) Del(key K)
- func (c *GenericCache[K]) Get(key K) (interface{}, bool)
- func (c *GenericCache[K]) GetInt64(key K) (int64, bool)
- func (c *GenericCache[K]) Put(key K, value interface{})
- func (c *GenericCache[K]) PutInt64(key K, value int64)
- func (c *GenericCache[K]) Stats() CacheStats
- type L1Config
- type L2Config
- type LFUCache
- func (c *LFUCache) Clear(ctx context.Context) error
- func (c *LFUCache) Close() error
- func (c *LFUCache) Delete(ctx context.Context, key string) error
- func (c *LFUCache) Exists(ctx context.Context, key string) bool
- func (c *LFUCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *LFUCache) GetFrequency(key string) (int64, bool)
- func (c *LFUCache) Len() int
- func (c *LFUCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *LFUCache) Stats() CacheStats
- type LRUCache
- func (c *LRUCache) Clear(ctx context.Context) error
- func (c *LRUCache) Close() error
- func (c *LRUCache) Delete(ctx context.Context, key string) error
- func (c *LRUCache) Exists(ctx context.Context, key string) bool
- func (c *LRUCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *LRUCache) GetOrSet(ctx context.Context, key string, fn func() (interface{}, error), ...) (interface{}, error)
- func (c *LRUCache) Keys() []string
- func (c *LRUCache) Len() int
- func (c *LRUCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *LRUCache) SetNX(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error)
- func (c *LRUCache) Stats() CacheStats
- func (c *LRUCache) Touch(ctx context.Context, key string) bool
- type MultiLevelCache
- func (c *MultiLevelCache) Clear(ctx context.Context) error
- func (c *MultiLevelCache) Close() error
- func (c *MultiLevelCache) Delete(ctx context.Context, key string) error
- func (c *MultiLevelCache) Demote(ctx context.Context, key string) error
- func (c *MultiLevelCache) Exists(ctx context.Context, key string) bool
- func (c *MultiLevelCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *MultiLevelCache) GetOrSet(ctx context.Context, key string, fn func() (interface{}, error), ...) (interface{}, error)
- func (c *MultiLevelCache) L1() *LRUCache
- func (c *MultiLevelCache) L2() *DiskCache
- func (c *MultiLevelCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *MultiLevelCache) SetL1Only(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *MultiLevelCache) SetL2Only(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *MultiLevelCache) Stats() MultiLevelStats
- type MultiLevelConfig
- type MultiLevelStats
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyNotFound = errors.New("key not found") ErrCacheFull = errors.New("cache is full") ErrKeyExpired = errors.New("key has expired") )
Common errors
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache.
Get(ctx context.Context, key string) (interface{}, error)
// Set stores a value in the cache with optional TTL.
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
// Delete removes a key from the cache.
Delete(ctx context.Context, key string) error
// Exists checks if a key exists in the cache.
Exists(ctx context.Context, key string) bool
// Clear removes all entries from the cache.
Clear(ctx context.Context) error
// Stats returns cache statistics.
Stats() CacheStats
// Close closes the cache and releases resources.
Close() error
}
Cache is the interface for cache implementations.
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
CacheManager manages named cache instances.
func NewCacheManager ¶
func NewCacheManager() *CacheManager
NewCacheManager creates a new cache manager.
func (*CacheManager) ClearAll ¶
func (m *CacheManager) ClearAll(ctx context.Context)
func (*CacheManager) Close ¶
func (m *CacheManager) Close()
func (*CacheManager) Register ¶
func (m *CacheManager) Register(name string, c Cache)
func (*CacheManager) Stats ¶
func (m *CacheManager) Stats() map[string]CacheStats
func (*CacheManager) Unregister ¶
func (m *CacheManager) Unregister(name string)
type CacheStats ¶
type CacheStats struct {
Hits int64 `json:"hits"`
Misses int64 `json:"misses"`
Sets int64 `json:"sets"`
Deletes int64 `json:"deletes"`
Evictions int64 `json:"evictions"`
Size int64 `json:"size"`
Capacity int64 `json:"capacity"`
HitRate float64 `json:"hit_rate"`
MemoryUsed int64 `json:"memory_used,omitempty"`
}
CacheStats holds cache statistics.
type Config ¶
type Config struct {
// MaxSize is the maximum number of entries.
MaxSize int `yaml:"max_size"`
// MaxMemory is the maximum memory usage in bytes (0 = unlimited).
MaxMemory int64 `yaml:"max_memory"`
// DefaultTTL is the default TTL for entries (0 = no expiration).
DefaultTTL time.Duration `yaml:"default_ttl"`
// EvictionPolicy is the eviction policy to use.
EvictionPolicy EvictionPolicy `yaml:"eviction_policy"`
// CleanupInterval is the interval for cleaning up expired entries.
CleanupInterval time.Duration `yaml:"cleanup_interval"`
// OnEvict is called when an entry is evicted.
OnEvict func(key string, value interface{})
}
Config holds cache configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default cache configuration.
type DiskCache ¶
type DiskCache struct {
// contains filtered or unexported fields
}
DiskCache implements a disk-based cache.
func NewDiskCache ¶
NewDiskCache creates a new disk cache.
type ECache ¶
type ECache struct {
// contains filtered or unexported fields
}
ECache wraps ecache.Cache with the Cache interface.
func NewECache ¶
NewECache creates a new ecache-based cache. bucketCount: number of buckets (shards) for concurrent access bucketSize: maximum items per bucket
func (*ECache) GetOrSet ¶
func (c *ECache) GetOrSet(ctx context.Context, key string, fn func() (interface{}, error)) (interface{}, error)
GetOrSet retrieves a value or sets it if not present.
func (*ECache) Set ¶
Set stores a value in the cache. TTL is determined by the DefaultTTL in config at cache creation time.
type ECache2 ¶
type ECache2 struct {
// contains filtered or unexported fields
}
ECache2 wraps ecache.Cache with LRU-2 mode for better hot data protection.
func NewECache2 ¶
NewECache2 creates a new ecache-based cache with LRU-2 mode. LRU-2 mode protects frequently accessed data from being evicted by bulk operations.
type Entry ¶
type Entry struct {
Key string
Value interface{}
Size int64
CreatedAt int64 // unix nanos
ExpiresAt int64 // unix nanos, 0 = no expiration
AccessedAt int64 // unix nanos
AccessCount int64
}
Entry represents a cache entry.
type EvictionPolicy ¶
type EvictionPolicy string
EvictionPolicy defines the cache eviction policy.
const ( // LRU evicts the least recently used entries. LRU EvictionPolicy = "lru" // LFU evicts the least frequently used entries. LFU EvictionPolicy = "lfu" // FIFO evicts the oldest entries first. FIFO EvictionPolicy = "fifo" )
type GenericCache ¶
type GenericCache[K genericCacheKey] struct {
// contains filtered or unexported fields
}
GenericCache is a lightweight generic cache with type-safe keys. K can be string, int, int64, uint64, etc.
func NewGenericCache ¶
func NewGenericCache[K genericCacheKey](config Config) *GenericCache[K]
NewGenericCache creates a new generic cache.
func NewGenericCacheWithStats ¶
func NewGenericCacheWithStats(config Config, poolName string) *GenericCache[string]
NewGenericCacheWithStats creates a new generic cache with local stats tracking.
func (*GenericCache[K]) Del ¶
func (c *GenericCache[K]) Del(key K)
Del removes a key from the cache.
func (*GenericCache[K]) Get ¶
func (c *GenericCache[K]) Get(key K) (interface{}, bool)
Get retrieves a value from the cache.
func (*GenericCache[K]) GetInt64 ¶
func (c *GenericCache[K]) GetInt64(key K) (int64, bool)
GetInt64 retrieves an int64 value from the cache.
func (*GenericCache[K]) Put ¶
func (c *GenericCache[K]) Put(key K, value interface{})
Put stores a value in the cache.
func (*GenericCache[K]) PutInt64 ¶
func (c *GenericCache[K]) PutInt64(key K, value int64)
PutInt64 stores an int64 value in the cache.
func (*GenericCache[K]) Stats ¶
func (c *GenericCache[K]) Stats() CacheStats
Stats returns local cache statistics.
type L1Config ¶
type L1Config struct {
Config
// ShardCount is the number of shards for concurrent access.
ShardCount int `yaml:"shard_count"`
}
L1Config holds L1 (memory) cache configuration.
func DefaultL1Config ¶
func DefaultL1Config() L1Config
DefaultL1Config returns the default L1 cache configuration.
type L2Config ¶
type L2Config struct {
Config
// Path is the directory path for disk cache.
Path string `yaml:"path"`
// MaxDiskSize is the maximum disk usage in bytes.
MaxDiskSize int64 `yaml:"max_disk_size"`
// Compression enables compression for stored values.
Compression bool `yaml:"compression"`
// CompressionLevel is the compression level (1-9).
CompressionLevel int `yaml:"compression_level"`
}
L2Config holds L2 (disk) cache configuration.
func DefaultL2Config ¶
func DefaultL2Config() L2Config
DefaultL2Config returns the default L2 cache configuration.
type LFUCache ¶
type LFUCache struct {
// contains filtered or unexported fields
}
LFUCache implements Cache with least-frequently-used eviction.
func (*LFUCache) GetFrequency ¶
GetFrequency returns the access frequency for a key.
func (*LFUCache) Stats ¶
func (c *LFUCache) Stats() CacheStats
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache implements Cache with least-recently-used eviction.
func (*LRUCache) Stats ¶
func (c *LRUCache) Stats() CacheStats
type MultiLevelCache ¶
type MultiLevelCache struct {
// contains filtered or unexported fields
}
MultiLevelCache combines L1 (memory) and L2 (disk) caches.
func NewMultiLevelCache ¶
func NewMultiLevelCache(config MultiLevelConfig) (*MultiLevelCache, error)
NewMultiLevelCache creates a new multi-level cache.
func (*MultiLevelCache) Close ¶
func (c *MultiLevelCache) Close() error
func (*MultiLevelCache) Delete ¶
func (c *MultiLevelCache) Delete(ctx context.Context, key string) error
func (*MultiLevelCache) Demote ¶
func (c *MultiLevelCache) Demote(ctx context.Context, key string) error
Demote moves a key from L1 to L2.
func (*MultiLevelCache) Exists ¶
func (c *MultiLevelCache) Exists(ctx context.Context, key string) bool
func (*MultiLevelCache) Get ¶
func (c *MultiLevelCache) Get(ctx context.Context, key string) (interface{}, error)
func (*MultiLevelCache) GetOrSet ¶
func (c *MultiLevelCache) GetOrSet(ctx context.Context, key string, fn func() (interface{}, error), ttl time.Duration) (interface{}, error)
GetOrSet retrieves a value or generates and stores it.
func (*MultiLevelCache) SetL1Only ¶
func (c *MultiLevelCache) SetL1Only(ctx context.Context, key string, value interface{}, ttl time.Duration) error
SetL1Only sets a value only in L1.
func (*MultiLevelCache) SetL2Only ¶
func (c *MultiLevelCache) SetL2Only(ctx context.Context, key string, value interface{}, ttl time.Duration) error
SetL2Only sets a value only in L2.
func (*MultiLevelCache) Stats ¶
func (c *MultiLevelCache) Stats() MultiLevelStats
type MultiLevelConfig ¶
type MultiLevelConfig struct {
L1 L1Config `yaml:"l1"`
L2 L2Config `yaml:"l2"`
// PromoteOnHit promotes entries from L2 to L1 on hit.
PromoteOnHit bool `yaml:"promote_on_hit"`
// WriteThrough writes to both L1 and L2 on set.
WriteThrough bool `yaml:"write_through"`
}
MultiLevelConfig holds multi-level cache configuration.
func DefaultMultiLevelConfig ¶
func DefaultMultiLevelConfig() MultiLevelConfig
DefaultMultiLevelConfig returns the default multi-level cache configuration.