redis

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package redis provides a Redis-backed distributed stats cache for mailbox.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a Redis Hash-backed distributed stats cache.

Each user's stats are stored as a Redis Hash at key "{prefix}:{userID}". Hash fields: "total", "unread", "draft", and per-folder "folder:{folderID}:total" / "folder:{folderID}:unread" entries.

HINCRBY keeps individual counters consistent without a read-modify-write cycle. HGETALL deserializes the full stats on a cache miss.

func New

func New(client redis.UniversalClient, opts ...Option) *Cache

New creates a new Redis stats cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, ownerID string) (*store.MailboxStats, error)

Get retrieves cached stats for a user. Returns nil, nil when not cached.

func (*Cache) IncrBy

func (c *Cache) IncrBy(ctx context.Context, ownerID string, field string, delta int64) error

IncrBy atomically increments a named field for a user. If the key does not exist (cache miss), the increment is skipped rather than seeding partial data — the next Get will miss and trigger a full store fetch.

func (*Cache) Invalidate

func (c *Cache) Invalidate(ctx context.Context, ownerID string) error

Invalidate removes cached stats for a user.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, ownerID string, stats *store.MailboxStats, ttl time.Duration) error

Set stores stats for a user with the given TTL.

type Option

type Option func(*options)

Option configures the Redis stats cache.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom logger.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the Redis key prefix. Default is "mailbox:stats".

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the cache entry TTL. Default is 5 minutes.

Jump to

Keyboard shortcuts

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