Documentation
¶
Index ¶
- type Cache
- type MemoryCache
- func (m *MemoryCache) Close() error
- func (m *MemoryCache) Delete(ctx context.Context, key string) error
- func (m *MemoryCache) DeletePattern(ctx context.Context, pattern string) error
- func (m *MemoryCache) Exists(ctx context.Context, key string) (bool, error)
- func (m *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
- func (m *MemoryCache) Len() int
- func (m *MemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type RedisCache
- func (r *RedisCache) Close() error
- func (r *RedisCache) Delete(ctx context.Context, key string) error
- func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error
- func (r *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (r *RedisCache) Get(ctx context.Context, key string) (interface{}, error)
- func (r *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type ShardedMemoryCache
- func (s *ShardedMemoryCache) Close() error
- func (s *ShardedMemoryCache) Delete(ctx context.Context, key string) error
- func (s *ShardedMemoryCache) DeletePattern(ctx context.Context, pattern string) error
- func (s *ShardedMemoryCache) Exists(ctx context.Context, key string) (bool, error)
- func (s *ShardedMemoryCache) Get(ctx context.Context, key string) (interface{}, error)
- func (s *ShardedMemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (s *ShardedMemoryCache) ShardCount() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string) (interface{}, error)
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Delete(ctx context.Context, key string) error
DeletePattern(ctx context.Context, pattern string) error
Exists(ctx context.Context, key string) (bool, error)
Close() error
}
Cache interface defines caching operations.
Two implementations are provided:
- MemoryCache: In-process LRU cache with per-item TTL support
- RedisCache: Distributed cache with per-item TTL support
Use MemoryCache for development and single-instance deployments. Use RedisCache for horizontal scaling.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an in-process LRU cache with per-item TTL.
Each call to Set may specify an independent TTL. A zero TTL means the item never expires (it may still be evicted if the cache is at capacity). A negative TTL is treated as zero (no expiry).
When capacity is reached, the least-recently-used item is evicted.
A background goroutine sweeps expired entries every sweepInterval. The goroutine is stopped when Close is called.
func NewMemoryCache ¶
func NewMemoryCache(capacity int, defaultTTL time.Duration) *MemoryCache
NewMemoryCache creates a new in-memory LRU cache.
capacity is the maximum number of items. When full, the LRU item is evicted. defaultTTL is used when Set is called with a zero TTL; pass 0 for no expiry.
func (*MemoryCache) Close ¶
func (m *MemoryCache) Close() error
Close stops the background sweeper and purges all entries.
func (*MemoryCache) Delete ¶
func (m *MemoryCache) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*MemoryCache) DeletePattern ¶
func (m *MemoryCache) DeletePattern(ctx context.Context, pattern string) error
DeletePattern removes all keys that have the given prefix (pattern must end with "*").
func (*MemoryCache) Get ¶
func (m *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves a value. Returns an error on miss or if the item has expired.
func (*MemoryCache) Len ¶
func (m *MemoryCache) Len() int
Len returns the number of non-expired items currently in the cache. Items that have expired but not yet been swept are excluded.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements a Redis-backed cache
func NewRedisCache ¶
func NewRedisCache(host string, port int, ttl time.Duration, poolSize int, minIdleConns int) (*RedisCache, error)
NewRedisCache creates a new Redis cache. poolSize and minIdleConns control the connection pool; zero values use defaults of 50 and 10 respectively.
func (*RedisCache) Delete ¶
func (r *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a key from Redis
func (*RedisCache) DeletePattern ¶
func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error
DeletePattern removes all keys matching a pattern
type ShardedMemoryCache ¶
type ShardedMemoryCache struct {
// contains filtered or unexported fields
}
ShardedMemoryCache distributes keys across multiple MemoryCache shards to reduce lock contention under concurrent access. Each shard has its own LRU and its own mutex, so operations on keys that hash to different shards never contend.
The total capacity is size (spread across shards), and TTL is global.
func NewShardedMemoryCache ¶
func NewShardedMemoryCache(size int, ttl time.Duration, shardCount int) *ShardedMemoryCache
NewShardedMemoryCache creates a sharded in-memory cache. size is the total capacity (divided across shards). shardCount must be a power of two; if zero or invalid, defaults to 16.
func (*ShardedMemoryCache) Close ¶
func (s *ShardedMemoryCache) Close() error
Close purges all shards.
func (*ShardedMemoryCache) Delete ¶
func (s *ShardedMemoryCache) Delete(ctx context.Context, key string) error
Delete removes a key from the appropriate shard.
func (*ShardedMemoryCache) DeletePattern ¶
func (s *ShardedMemoryCache) DeletePattern(ctx context.Context, pattern string) error
DeletePattern removes all keys matching a prefix pattern across all shards.
func (*ShardedMemoryCache) Get ¶
func (s *ShardedMemoryCache) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves a value from the appropriate shard.
func (*ShardedMemoryCache) Set ¶
func (s *ShardedMemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Set stores a value in the appropriate shard.
func (*ShardedMemoryCache) ShardCount ¶
func (s *ShardedMemoryCache) ShardCount() int
ShardCount returns the number of shards (useful for diagnostics).