secondarystorage

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedisConfigURLNotProvided = errors.New("redis secondary storage configuration URL not provided")
	ErrDatabaseConfigNotProvided = errors.New("database secondary storage configuration not provided")
)

Functions

func NewSecondaryStorageService

func NewSecondaryStorageService(providerName string, storage models.SecondaryStorage) services.SecondaryStorageService

NewSecondaryStorageService creates a new SecondaryStorageService instance

Types

type DatabaseSecondaryStorage

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

DatabaseSecondaryStorage implements the SecondaryStorage interface using Bun ORM.

func NewDatabaseSecondaryStorage

func NewDatabaseSecondaryStorage(db bun.IDB, config DatabaseStorageConfig) *DatabaseSecondaryStorage

func (*DatabaseSecondaryStorage) Close

func (storage *DatabaseSecondaryStorage) Close() error

Close gracefully shuts down the storage by stopping the cleanup goroutine. This method is idempotent and safe to call multiple times.

func (*DatabaseSecondaryStorage) Delete

func (storage *DatabaseSecondaryStorage) Delete(ctx context.Context, key string) error

Delete removes a key from the database. It is idempotent: deleting a non-existent key does not return an error.

func (*DatabaseSecondaryStorage) Get

func (storage *DatabaseSecondaryStorage) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from the database by key. Returns nil if the key does not exist or has expired. Expired entries are deleted immediately to prevent database bloat.

func (*DatabaseSecondaryStorage) Incr

func (storage *DatabaseSecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)

Incr increments the integer value stored at key by 1. If the key does not exist, it is initialized to 0 and then incremented to 1. If ttl is provided, it will be set or updated on the key. Expired entries are deleted immediately before incrementing.

func (*DatabaseSecondaryStorage) Set

func (storage *DatabaseSecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error

Set stores a value in the database with an optional TTL. The value must be a string. If ttl is nil, the entry will not expire.

func (*DatabaseSecondaryStorage) StartCleanup

func (storage *DatabaseSecondaryStorage) StartCleanup()

StartCleanup starts the background cleanup goroutine that removes expired entries. This should be called after database migrations have completed. It is safe to call this multiple times - subsequent calls will be no-ops.

func (*DatabaseSecondaryStorage) TTL

func (storage *DatabaseSecondaryStorage) TTL(ctx context.Context, key string) (*time.Duration, error)

TTL returns the remaining time to live for a key Returns nil if the key does not exist or has no expiration

type DatabaseStorageConfig

type DatabaseStorageConfig struct {
	// CleanupInterval controls how often expired entries are cleaned up
	CleanupInterval time.Duration `json:"cleanup_interval" toml:"cleanup_interval"`
}

DatabaseStorageConfig contains configuration for database storage provider

type KeyValueStore

type KeyValueStore struct {
	bun.BaseModel `bun:"table:key_value_store"`

	Key       string     `json:"key" bun:"column:key,pk"`
	Value     string     `json:"value" bun:"column:value"`
	ExpiresAt *time.Time `json:"expires_at" bun:"column:expires_at"`
	CreatedAt time.Time  `json:"created_at" bun:"column:created_at,default:current_timestamp"`
	UpdatedAt time.Time  `json:"updated_at" bun:"column:updated_at,default:current_timestamp"`
}

KeyValueStore represents the persistent key-value store table in the database. This is a domain model used for secondary storage operations.

type MemorySecondaryStorage

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

MemorySecondaryStorage is an in-memory implementation of SecondaryStorage.

func NewMemorySecondaryStorage

func NewMemorySecondaryStorage(config MemoryStorageConfig) *MemorySecondaryStorage

func (*MemorySecondaryStorage) Close

func (storage *MemorySecondaryStorage) Close() error

Close gracefully shuts down the storage by stopping the cleanup goroutine. This method is idempotent and safe to call multiple times.

func (*MemorySecondaryStorage) Delete

func (storage *MemorySecondaryStorage) Delete(ctx context.Context, key string) error

Delete removes a key from storage. This operation is idempotent: no error is returned if the key does not exist.

func (*MemorySecondaryStorage) Get

func (storage *MemorySecondaryStorage) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from memory by key. Returns nil if the key does not exist or has expired. Expired entries are deleted immediately to prevent memory bloat.

func (*MemorySecondaryStorage) Incr

func (storage *MemorySecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)

Incr increments the integer value stored at key by 1. If the key does not exist, it is initialized to 0 and then incremented to 1. If ttl is provided, it will be set or updated on the key. Expired entries are deleted immediately before incrementing.

func (*MemorySecondaryStorage) Set

func (storage *MemorySecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error

Set stores a value in memory with an optional TTL. The value must be a string. If ttl is nil, the entry will not expire.

func (*MemorySecondaryStorage) TTL

func (storage *MemorySecondaryStorage) TTL(ctx context.Context, key string) (*time.Duration, error)

TTL returns the remaining time to live for a key Returns nil if the key does not exist or has no expiration

type MemoryStorageConfig

type MemoryStorageConfig struct {
	// CleanupInterval controls how often expired entries are cleaned up
	CleanupInterval time.Duration `json:"cleanup_interval" toml:"cleanup_interval"`
}

MemoryStorageConfig contains configuration for in-memory storage provider

type RedisSecondaryStorage

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

RedisSecondaryStorage implements SecondaryStorage using Redis as the backend

func NewRedisSecondaryStorage

func NewRedisSecondaryStorage(opts RedisSecondaryStorageOptions) (*RedisSecondaryStorage, error)

NewRedisSecondaryStorage creates a new Redis-backed secondary storage instance

func (*RedisSecondaryStorage) Close

func (rs *RedisSecondaryStorage) Close() error

Close closes the Redis connection

func (*RedisSecondaryStorage) Delete

func (rs *RedisSecondaryStorage) Delete(ctx context.Context, key string) error

Delete removes a key from Redis

func (*RedisSecondaryStorage) Get

func (rs *RedisSecondaryStorage) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from Redis by key Returns nil if the key does not exist

func (*RedisSecondaryStorage) Incr

func (rs *RedisSecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)

Incr atomically increments an integer value in Redis by 1 If the key does not exist, it is set to 1 If a TTL is provided, it is only applied on key creation

func (*RedisSecondaryStorage) Set

func (rs *RedisSecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error

Set stores a value in Redis with an optional TTL The value must be a string. TODO: update this to support other types as needed

func (*RedisSecondaryStorage) TTL

TTL returns the remaining time to live for a key in seconds Returns nil if the key does not exist or has no expiration

type RedisSecondaryStorageOptions

type RedisSecondaryStorageOptions struct {
	URL         string
	MaxRetries  int
	PoolSize    int
	PoolTimeout time.Duration
}

RedisSecondaryStorageOptions configures a Redis secondary storage instance

type RedisStorageConfig

type RedisStorageConfig struct {
	// URL is the Redis connection URL (e.g., "redis://[username:password@]host[:port]/[db]")
	URL string `json:"url" toml:"url"`
	// MaxRetries is the maximum number of retries for Redis operations
	MaxRetries int `json:"max_retries" toml:"max_retries"`
	// PoolSize is the connection pool size for Redis
	PoolSize int `json:"pool_size" toml:"pool_size"`
	// PoolTimeout is the timeout for getting a connection from the pool
	PoolTimeout time.Duration `json:"pool_timeout" toml:"pool_timeout"`
}

RedisStorageConfig contains configuration for Redis storage provider

type SecondaryStorageAPI

type SecondaryStorageAPI interface {
	// GetStorage returns the configured SecondaryStorage backend
	GetStorage() models.SecondaryStorage
	// GetProviderName returns the name of the currently active provider
	GetProviderName() string
}

SecondaryStorageAPI is the API exposed by the secondary storage plugin

type SecondaryStoragePlugin

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

func New

New creates a new SecondaryStoragePlugin with the given configuration

func NewWithStorage

func NewWithStorage(providerName string, storage models.SecondaryStorage) *SecondaryStoragePlugin

NewWithStorage creates a SecondaryStoragePlugin with a custom SecondaryStorage implementation This allows library mode users to provide their own storage backend

func (*SecondaryStoragePlugin) Close

func (p *SecondaryStoragePlugin) Close() error

func (*SecondaryStoragePlugin) Config

func (p *SecondaryStoragePlugin) Config() any

func (*SecondaryStoragePlugin) DependsOn added in v2.8.0

func (p *SecondaryStoragePlugin) DependsOn() []string

func (*SecondaryStoragePlugin) Init

Init initializes the secondary storage plugin It attempts to initialize the configured provider and falls back to in-memory if that fails If a custom storage implementation was provided via NewWithStorage, this becomes a no-op

func (*SecondaryStoragePlugin) Metadata

func (*SecondaryStoragePlugin) Migrations

func (p *SecondaryStoragePlugin) Migrations(provider string) []migrations.Migration

func (*SecondaryStoragePlugin) OnConfigUpdate

func (p *SecondaryStoragePlugin) OnConfigUpdate(config *models.Config) error

type SecondaryStoragePluginConfig

type SecondaryStoragePluginConfig struct {
	// Enabled controls whether secondary storage is available
	Enabled bool `json:"enabled" toml:"enabled"`

	// Provider specifies which storage backend to use: "memory", "database", or "redis"
	// Defaults to "memory" if not specified or if the selected provider is not available
	Provider SecondaryStorageProvider `json:"provider" toml:"provider"`

	// Memory provider configuration
	Memory *MemoryStorageConfig `json:"memory" toml:"memory"`

	// Database provider configuration
	Database *DatabaseStorageConfig `json:"database" toml:"database"`

	// Redis provider configuration
	Redis *RedisStorageConfig `json:"redis" toml:"redis"`
}

SecondaryStoragePluginConfig is the main configuration for the secondary storage plugin Defaults to in-memory storage if no provider is configured

func (*SecondaryStoragePluginConfig) ApplyDefaults

func (config *SecondaryStoragePluginConfig) ApplyDefaults()

type SecondaryStorageProvider

type SecondaryStorageProvider string
const (
	SecondaryStorageProviderMemory   SecondaryStorageProvider = "memory"
	SecondaryStorageProviderDatabase SecondaryStorageProvider = "database"
	SecondaryStorageProviderRedis    SecondaryStorageProvider = "redis"
)

func (SecondaryStorageProvider) String

func (p SecondaryStorageProvider) String() string

type SecondaryStorageServiceImpl

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

SecondaryStorageServiceImpl implements the SecondaryStorageService interface

func (*SecondaryStorageServiceImpl) GetProviderName

func (s *SecondaryStorageServiceImpl) GetProviderName() string

GetProviderName returns the name of the currently active provider

func (*SecondaryStorageServiceImpl) GetStorage

GetStorage returns the configured SecondaryStorage backend

Jump to

Keyboard shortcuts

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