Documentation
¶
Overview ¶
Package cache provides a multi-level caching system and distributed lock implementation.
It defines a unified Cache interface that can be implemented by both local memory engines (like Ristretto) and remote distributed engines (like Redis). It also provides a DistributedLock interface for concurrency control across nodes.
Basic usage:
localCache, _ := cache.NewMemoryCache(100 * 1024 * 1024) // 100MB localCache.Set(ctx, "key", "value", 5*time.Minute) val, err := localCache.Get(ctx, "key")
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLockAcquisitionFailed is returned when the lock is already held by another client. ErrLockAcquisitionFailed = errors.New("cache: failed to acquire distributed lock") )
var ( // ErrNotFound is returned when a requested key does not exist in the cache. ErrNotFound = errors.New("cache: key not found") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache. Returns an error (e.g., ErrNotFound) if not found.
Get(ctx context.Context, key string) (string, error)
// Set stores a value in the cache with an optional Time-To-Live (TTL).
// A TTL of 0 means the key does not expire.
Set(ctx context.Context, key string, value string, ttl time.Duration) error
// Delete removes a key from the cache.
Delete(ctx context.Context, key string) error
// Clear completely flushes the cache.
Clear(ctx context.Context) error
}
Cache defines the standard behavior for a caching engine (either local or remote).
type DistributedLock ¶
type DistributedLock interface {
// Acquire attempts to acquire the lock with a given TTL.
// Returns true if successfully acquired, false otherwise.
Acquire(ctx context.Context, key string, ttl time.Duration) (bool, error)
// Release releases the lock.
Release(ctx context.Context, key string) error
// Extend attempts to extend the TTL of a lock currently held by the caller.
Extend(ctx context.Context, key string, ttl time.Duration) (bool, error)
}
DistributedLock defines the behavior for a cluster-wide mutual exclusion lock.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements the Cache interface using dgraph-io/ristretto.
func NewMemoryCache ¶
func NewMemoryCache(maxCostBytes int64) (*MemoryCache, error)
NewMemoryCache creates a new in-memory cache with bounded max cost (in bytes). Note: It is highly concurrent and uses TinyLFU for optimal cache admission.
func (*MemoryCache) Clear ¶
func (m *MemoryCache) Clear(ctx context.Context) error
Clear completely flushes the cache.
func (*MemoryCache) Delete ¶
func (m *MemoryCache) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements the Cache interface using Redis.
func NewRedisCache ¶
func NewRedisCache(client redis.UniversalClient) *RedisCache
NewRedisCache creates a new remote cache instance using a provided Redis client. This allows reusing the connection pool from the existing db/redis module.
func (*RedisCache) Clear ¶
func (r *RedisCache) Clear(ctx context.Context) error
Clear flushes the current Redis database. WARNING: Use with caution.
func (*RedisCache) Delete ¶
func (r *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a key from Redis.
type RedisLock ¶
type RedisLock struct {
// contains filtered or unexported fields
}
RedisLock implements DistributedLock using Redis SETNX with a random token to prevent accidental release.
func NewRedisLock ¶
func NewRedisLock(client redis.UniversalClient) *RedisLock
NewRedisLock creates a new DistributedLock instance.
func (*RedisLock) Acquire ¶
Acquire attempts to acquire a lock for a given key with a specified TTL.