cache

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides in-memory caching implementations.

Package cache provides Redis-based caching implementations.

Index

Constants

This section is empty.

Variables

View Source
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.

func (*MemoryCache) Get

func (c *MemoryCache) Get(_ context.Context, key string) (interface{}, error)

Get retrieves a value from the memory cache by key. It returns an error if the key does not exist or has expired.

func (*MemoryCache) Set

func (c *MemoryCache) Set(_ context.Context, key string, value interface{}, ttl time.Duration) error

Set stores a value in the memory cache with an optional TTL. If TTL is greater than zero, the key will expire after the duration.

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.

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) (interface{}, error)

Get retrieves a value from Redis by key. It returns an error if the key does not exist or if there is a failure in retrieval.

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set stores a value in Redis with an optional TTL. It marshals the value to JSON before storing.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL