cache

package
v0.0.0-...-5ed4770 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache defines the caching and pub/sub layer for sequence numbers, connection counts, and real-time message distribution.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChannelClosed is returned when a subscribed channel has been closed
	// (either by calling the unsubscribe func or due to connection drop).
	ErrChannelClosed = errors.New("channel closed")
)

Sentinel errors for cache operations.

These errors are used with fmt.Errorf("%w", ErrXxx) to preserve the error chain. Callers use errors.Is(err, ErrChannelClosed) to check error types.

Functions

This section is empty.

Types

type Cache

type Cache interface {

	// IncrSeq atomically increments and returns the sequence number for a given scope.
	IncrSeq(ctx context.Context, scope, scopeID string) (uint64, error)

	// IncrMsgID atomically increments and returns the message ID for a conversation.
	IncrMsgID(ctx context.Context, conversationID string) (uint64, error)

	// IncrSeqAndMsgID atomically increments both seq and msgID in a single call.
	IncrSeqAndMsgID(ctx context.Context, scope, scopeID, conversationID string) (seq uint64, msgID uint64, err error)

	// GetCurrentSeq returns the current sequence number for a given scope without incrementing.
	GetCurrentSeq(ctx context.Context, scope, scopeID string) (uint64, error)

	// SetSeq sets the sequence counter for a scope to the given value.
	// Used during startup to sync Redis seq with DB max(seq).
	// Only updates if val > current seq (never goes backwards).
	SetSeq(ctx context.Context, scope, scopeID string, val uint64) error

	// Publish sends data to the specified channel.
	Publish(ctx context.Context, channel string, data []byte) error

	// Subscribe returns a channel that receives messages from the specified channel.
	// The returned func() is the unsubscribe function — calling it closes the channel
	// and releases resources. There is no standalone Unsubscribe method.
	Subscribe(ctx context.Context, channel string) (<-chan []byte, func(), error)

	// SetConversationState stores the state of a conversation.
	SetConversationState(ctx context.Context, conversationID string, state string) error

	// GetConversationState retrieves the state of a conversation.
	GetConversationState(ctx context.Context, conversationID string) (string, error)

	// IncrConnCount atomically increments the connection count for a user and sets TTL.
	// Returns the new count after increment.
	IncrConnCount(ctx context.Context, userID string) (int64, error)

	// DecrConnCount atomically decrements the connection count for a user.
	// Clamps at 0 (no negative counts). Returns the new count after decrement.
	DecrConnCount(ctx context.Context, userID string) (int64, error)

	// GetConnCount returns the current connection count for a user.
	// Returns 0 if no connections exist.
	GetConnCount(ctx context.Context, userID string) (int64, error)

	// RenewConnCount extends the TTL for a user's connection count without changing the count.
	// Used for batch TTL renewal to prevent expiry of active users.
	RenewConnCount(ctx context.Context, userID string) error

	// IsHealthy returns true if the cache backend is reachable.
	IsHealthy(ctx context.Context) bool

	// Close releases all resources (connections, goroutines).
	Close() error
}

Cache defines the caching and pub/sub layer for sequence numbers, connection counts, and real-time message distribution. Implementations must support atomic increment operations for seq allocation.

Default implementation: Redis.

Directories

Path Synopsis
Package memory provides an in-memory implementation of the cache.Cache interface for development and testing environments.
Package memory provides an in-memory implementation of the cache.Cache interface for development and testing environments.
Package redis provides a Redis-backed implementation of the cache.Cache interface.
Package redis provides a Redis-backed implementation of the cache.Cache interface.

Jump to

Keyboard shortcuts

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