redis

package module
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

redis

Package redis is a history Store backed by Redis via go-redis. Each conversation maps to a Redis list keyed by <KeyPrefix><conversationID> (default prefix chat:history:). Messages are RPUSH'd as canonical chat.Message JSON, so a LRANGE 0 -1 preserves list order. When TTL is configured, append and expiry refresh execute in one Redis transaction. Example: client := goredis.NewUniversalClient(&goredis.UniversalOptions{...}) store, _ := redis.NewStore(redis.StoreConfig{Client: client})

Install

go get github.com/Tangerg/scope/historystores/redis

Constructors

Every constructor validates its config and returns a value implementing the store capabilities in core/history:

  • NewStore

Testing

This module integrates a third-party service, so its tests cover what runs without live credentials: config validation, request and response mapping, and error classification. The shared conformance contract is core/history/storetest — this module runs it rather than copying it.

An integration probe skips unless its credential environment variable is set, so go test ./... is always runnable offline.

Boundaries

This is an independent leaf module: it carries only its own SDK dependency and never imports a sibling provider. The shared contract every module in this family obeys is in ../ARCHITECTURE.md.

See ARCHITECTURE.md for what this module owns.

Documentation

Overview

Package redis is a history Store backed by Redis via go-redis.

Each conversation maps to a Redis list keyed by `<KeyPrefix><conversationID>` (default prefix `chat:history:`). Messages are RPUSH'd as canonical chat.Message JSON, so a LRANGE 0 -1 preserves list order. When TTL is configured, append and expiry refresh execute in one Redis transaction.

Example:

client := goredis.NewUniversalClient(&goredis.UniversalOptions{...})
store, _ := redis.NewStore(redis.StoreConfig{Client: client})

Index

Constants

View Source
const DefaultKeyPrefix = "chat:history:"

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store persists each conversation as an ordered Redis list through a caller-owned client. It never closes the client; when TTL is configured, append and expiry refresh share one transaction so retention cannot lag a successful write.

func NewStore

func NewStore(config StoreConfig) (*Store, error)

func (*Store) Clear

func (s *Store) Clear(ctx context.Context, conversationID history.ConversationID) (err error)

Clear drops the entire list for conversationID. Unknown ids are silently ignored (DEL on a missing key is a no-op in Redis).

func (*Store) Conversations

func (s *Store) Conversations(ctx context.Context) (ids []history.ConversationID, err error)

Conversations enumerates stored conversation IDs via SCAN and returns them in lexical order. SCAN may observe concurrent mutations and repeat keys, so results are de-duplicated.

func (*Store) Read

func (s *Store) Read(ctx context.Context, conversationID history.ConversationID) (storedMessages []chat.Message, err error)

Read returns every message stored under conversationID in insertion order. An empty slice is returned for unknown ids.

func (*Store) Write

func (s *Store) Write(ctx context.Context, conversationID history.ConversationID, messages ...chat.Message) (err error)

Write appends every message under conversationID. When TTL is set, append and expiry refresh execute in one Redis transaction. Empty writes are a no-op.

type StoreConfig

type StoreConfig struct {
	// Client is the live go-redis client. Required. The store does
	// not take ownership — callers Close() the client themselves.
	Client goredis.UniversalClient

	// KeyPrefix is prepended to every conversation id to namespace the
	// keys. Optional: defaults to [DefaultKeyPrefix].
	KeyPrefix string

	// TTL, when non-zero, applies a millisecond-precision expiry to every
	// conversation key and refreshes it on each Write. Zero means "never
	// expire".
	TTL time.Duration
}

func (StoreConfig) Validate

func (s StoreConfig) Validate() error

Jump to

Keyboard shortcuts

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