Documentation
¶
Overview ¶
Package cache provides in-memory caching implementations.
Package cache provides Redis-based caching implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("key not found") ErrKeyExpired = errors.New("key expired") ErrCacheFailure = errors.New("cache failure") )
Define cache-specific errors
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
Close() error
}
Cache defines the methods required for a caching backend.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements the Cache interface using an in-memory store.
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache initializes a new MemoryCache instance. It starts a garbage collection goroutine to clean expired items.
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close() error
Close clears all items from the memory cache.
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(_ context.Context, key string) error
Delete removes a key from the memory cache.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements the Cache interface using Redis.
func NewRedisCache ¶
func NewRedisCache(addr string, password string, db int) (*RedisCache, error)
NewRedisCache initializes a new RedisCache instance. It connects to the Redis server at the specified address with the given password and DB number.
func (*RedisCache) Close ¶
func (c *RedisCache) Close() error
Close closes the Redis client connection.
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a key from Redis. It returns an error if the deletion fails.
type RedisClient ¶
type RedisClient interface {
Get(ctx context.Context, key string) *redis.StringCmd
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
Close() error
}
RedisClient defines the methods used from redis.Client