memory

package
v0.0.0-...-ba96703 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2025 License: MIT Imports: 10 Imported by: 0

README

Memory Interface

This package provides a common interface for key-value storage backends, with implementations for SQLite and Redis.

Usage

import "github.com/scottdavis/dsgo/pkg/agents/memory"

// Create a SQLite-backed memory store (in-memory or file-based)
sqliteStore, err := memory.NewSQLiteStore(":memory:")
if err != nil {
    return err
}
defer sqliteStore.Close()

// Create a Redis-backed memory store
redisStore, err := memory.NewRedisStore("localhost:6379", "", 0)
if err != nil {
    return err
}
defer redisStore.Close()

// Store values
err = sqliteStore.Store("key", "value")
if err != nil {
    return err
}

// Retrieve values
value, err := sqliteStore.Retrieve("key")
if err != nil {
    return err
}

// Store with TTL (time-to-live)
ctx := context.Background()
err = redisStore.StoreWithTTL(ctx, "key", "value", 5*time.Minute)
if err != nil {
    return err
}

Testing

Local Testing

SQLite tests run in all environments. Redis tests are skipped by default in local environments.

To run Redis tests locally, you need to:

  1. Start a Redis server (e.g., using Docker)
  2. Set the REDIS_TEST_ADDR environment variable to point to your Redis server
  3. Optionally set REDIS_TEST_PASSWORD if your Redis server requires authentication
# Start Redis with Docker
docker run -d --name redis-test -p 6379:6379 redis:latest

# Run tests with Redis
REDIS_TEST_ADDR=localhost:6379 go test -v ./pkg/agents/memory/...
CI Setup

For running tests in CI, you need to add Redis to your CI pipeline. Here's an example setup for GitHub Actions:

# .github/workflows/test.yml
name: Tests

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      redis:
        image: redis:latest
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: '1.21'
    
    - name: Install dependencies
      run: go mod download
    
    - name: Run tests
      run: go test -v ./...
      env:
        REDIS_TEST_ADDR: localhost:6379

For other CI systems (like GitLab CI, Circle CI, etc.), the approach is similar - you need to:

  1. Add a Redis service to your CI configuration
  2. Set the REDIS_TEST_ADDR environment variable to point to the Redis service
  3. Optionally set REDIS_TEST_PASSWORD if needed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMemoryStore

func GetMemoryStore(ctx context.Context) (agents.Memory, error)

GetMemoryStore retrieves the memory store from the context

func WithMemoryStore

func WithMemoryStore(ctx context.Context, store agents.Memory) context.Context

WithMemoryStore adds a memory store to the context

func WithRedisStore

func WithRedisStore(ctx context.Context, store *RedisStore) context.Context

WithRedisStore adds a Redis memory store to the context This is an alias for WithMemoryStore for clarity when using Redis specifically

Types

type Memory

type Memory interface {
	// Store saves a value with the specified key.
	// Optional StoreOption parameters can be provided to configure the storage behavior,
	// such as TTL (time-to-live).
	Store(key string, value any, opts ...agents.StoreOption) error

	// Retrieve gets a value by its key.
	Retrieve(key string) (any, error)

	// List returns all keys in the store.
	List() ([]string, error)

	// Clear removes all values from the store.
	Clear() error

	// CleanExpired removes all expired entries from the store.
	CleanExpired(ctx context.Context) (int64, error)

	// Close releases resources used by the memory store.
	Close() error
}

Memory defines the interface for key-value storage backends.

type RedisStore

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

RedisStore implements the Memory interface using Redis as the backend.

func GetRedisStore

func GetRedisStore(ctx context.Context) (*RedisStore, error)

GetRedisStore retrieves the Redis memory store from the context

func NewRedisStore

func NewRedisStore(addr, password string, db int) (*RedisStore, error)

NewRedisStore creates a new Redis-backed memory store.

func (*RedisStore) CleanExpired

func (r *RedisStore) CleanExpired(ctx context.Context) (int64, error)

CleanExpired implements the Memory interface CleanExpired method. Note: Redis automatically removes expired keys, so this is a no-op.

func (*RedisStore) Clear

func (r *RedisStore) Clear() error

Clear implements the Memory interface Clear method.

func (*RedisStore) Close

func (r *RedisStore) Close() error

Close implements the Memory interface Close method.

func (*RedisStore) List

func (r *RedisStore) List() ([]string, error)

List implements the Memory interface List method.

func (*RedisStore) Retrieve

func (r *RedisStore) Retrieve(key string) (any, error)

Retrieve implements the Memory interface Retrieve method.

func (*RedisStore) Store

func (r *RedisStore) Store(key string, value any, opts ...agents.StoreOption) error

Store implements the Memory interface Store method.

func (*RedisStore) StoreWithTTL

func (r *RedisStore) StoreWithTTL(ctx context.Context, key string, value any, ttl time.Duration) error

StoreWithTTL implements the Memory interface StoreWithTTL method.

type SQLiteStore

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

SQLiteStore implements the Memory interface using SQLite as the backend.

func NewSQLiteStore

func NewSQLiteStore(path string) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-backed memory store. The path parameter specifies the database file location. If path is ":memory:", the database will be created in-memory.

func (*SQLiteStore) CleanExpired

func (s *SQLiteStore) CleanExpired(ctx context.Context) (int64, error)

CleanExpired removes all expired entries from the store.

func (*SQLiteStore) Clear

func (s *SQLiteStore) Clear() error

Clear implements the Memory interface Clear method.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the database connection.

func (*SQLiteStore) List

func (s *SQLiteStore) List() ([]string, error)

List implements the Memory interface List method.

func (*SQLiteStore) Retrieve

func (s *SQLiteStore) Retrieve(key string) (any, error)

Retrieve implements the Memory interface Retrieve method.

func (*SQLiteStore) Store

func (s *SQLiteStore) Store(key string, value any, opts ...agents.StoreOption) error

Store implements the Memory interface Store method.

type StoreOption

type StoreOption func(*StoreOptions)

StoreOption defines options for Store operations

func WithTTL

func WithTTL(ttl time.Duration) StoreOption

WithTTL creates an option to set a TTL for a stored value

type StoreOptions

type StoreOptions struct {
	TTL time.Duration
}

StoreOptions contains configuration for Store operations

Jump to

Keyboard shortcuts

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