Documentation
¶
Overview ¶
Package cache provides a cache interface and implementations for caching data.
Index ¶
- Constants
- func HashToken(token string) string
- func SessionKey(userID, tokenHash string) string
- func TokenKey(token string) string
- func UserKey(userID string) string
- type Cache
- type NoOpCache
- func (n *NoOpCache) Close() error
- func (n *NoOpCache) Delete(ctx context.Context, key string) error
- func (n *NoOpCache) Exists(ctx context.Context, key string) (bool, error)
- func (n *NoOpCache) Get(ctx context.Context, key string) (string, bool, error)
- func (n *NoOpCache) Ping(ctx context.Context) error
- func (n *NoOpCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error
- type RedisCache
- func (r *RedisCache) Client() *redis.Client
- func (r *RedisCache) Close() error
- func (r *RedisCache) Delete(ctx context.Context, key string) error
- func (r *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (r *RedisCache) Get(ctx context.Context, key string) (string, bool, error)
- func (r *RedisCache) Ping(ctx context.Context) error
- func (r *RedisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error
- type RedisConfig
Constants ¶
const ( PrefixSession = "session:" PrefixUser = "user:" PrefixToken = "token:" )
Cache key prefixes
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 SessionKey ¶
SessionKey generates a cache key for user sessions. Format: session:{user_id}:{token_hash_prefix}
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.
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) Delete ¶
func (r *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a value by key.
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.