cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cache provides a cache interface and implementations for caching data.

Index

Constants

View Source
const (
	PrefixSession = "session:"
	PrefixUser    = "user:"
	PrefixToken   = "token:"
)

Cache key prefixes

View Source
const (
	SessionTTL     = 24 * time.Hour // Match JWT expiry
	UserProfileTTL = 15 * time.Minute
	TokenTTL       = 24 * time.Hour
)

Default TTLs

Variables

This section is empty.

Functions

func HashToken

func HashToken(token string) string

HashToken creates a SHA-256 hash of a token string.

func SessionKey

func SessionKey(userID, tokenHash string) string

SessionKey generates a cache key for user sessions. Format: session:{user_id}:{token_hash_prefix}

func TokenKey

func TokenKey(token string) string

TokenKey generates a cache key for token validation. Format: token:{token_hash}

func UserKey

func UserKey(userID string) string

UserKey generates a cache key for user profiles. Format: user:{user_id}

Types

type Cache

type Cache interface {
	// Get retrieves a value by key. Returns empty string and false if not found.
	Get(ctx context.Context, key string) (string, bool, error)

	// Set stores a value with the given key and TTL.
	Set(ctx context.Context, key string, value string, ttl time.Duration) error

	// Delete removes a value by key.
	Delete(ctx context.Context, key string) error

	// Exists checks if a key exists in the cache.
	Exists(ctx context.Context, key string) (bool, error)

	// Ping checks the connection health.
	Ping(ctx context.Context) error

	// Close closes the cache connection.
	Close() error
}

Cache defines the interface for cache operations. All operations should be context-aware and support graceful degradation.

type NoOpCache

type NoOpCache struct{}

NoOpCache is a cache implementation that does nothing. Useful when caching is disabled or as a fallback.

func NewNoOpCache

func NewNoOpCache() *NoOpCache

NewNoOpCache creates a new no-op cache.

func (*NoOpCache) Close

func (n *NoOpCache) Close() error

func (*NoOpCache) Delete

func (n *NoOpCache) Delete(ctx context.Context, key string) error

func (*NoOpCache) Exists

func (n *NoOpCache) Exists(ctx context.Context, key string) (bool, error)

func (*NoOpCache) Get

func (n *NoOpCache) Get(ctx context.Context, key string) (string, bool, error)

func (*NoOpCache) Ping

func (n *NoOpCache) Ping(ctx context.Context) error

func (*NoOpCache) Set

func (n *NoOpCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache implements Cache using Redis as the backend.

func NewRedisCache

func NewRedisCache(cfg *RedisConfig) (*RedisCache, error)

NewRedisCache creates a new Redis cache client. Returns an error if the connection cannot be established.

func (*RedisCache) Client

func (r *RedisCache) Client() *redis.Client

Client returns the underlying Redis client for advanced operations.

func (*RedisCache) Close

func (r *RedisCache) Close() error

Close closes the Redis connection.

func (*RedisCache) Delete

func (r *RedisCache) Delete(ctx context.Context, key string) error

Delete removes a value by key.

func (*RedisCache) Exists

func (r *RedisCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache.

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) (string, bool, error)

Get retrieves a value by key.

func (*RedisCache) Ping

func (r *RedisCache) Ping(ctx context.Context) error

Ping checks the connection health.

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error

Set stores a value with the given key and TTL.

type RedisConfig

type RedisConfig struct {
	URL             string        // Redis connection URL (redis://:password@host:port/db)
	MaxRetries      int           // Maximum number of retries (default: 3)
	DialTimeout     time.Duration // Connection timeout (default: 5s)
	ReadTimeout     time.Duration // Read timeout (default: 3s)
	WriteTimeout    time.Duration // Write timeout (default: 3s)
	PoolSize        int           // Connection pool size (default: 10)
	MinIdleConns    int           // Minimum idle connections (default: 2)
	MaxIdleTime     time.Duration // Max idle time before closing (default: 5m)
	ConnMaxLifetime time.Duration // Max connection lifetime (default: 30m)
}

RedisConfig holds Redis connection configuration.

func DefaultRedisConfig

func DefaultRedisConfig(url string) *RedisConfig

DefaultRedisConfig returns a RedisConfig with sensible defaults.

Jump to

Keyboard shortcuts

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