Documentation
¶
Index ¶
- Variables
- func GetMigrations(ctx context.Context, provider string) (*embed.FS, error)
- func NewSecondaryStorageService(providerName string, storage models.SecondaryStorage) services.SecondaryStorageService
- type DatabaseSecondaryStorage
- func (storage *DatabaseSecondaryStorage) Close() error
- func (storage *DatabaseSecondaryStorage) Delete(ctx context.Context, key string) error
- func (storage *DatabaseSecondaryStorage) Get(ctx context.Context, key string) (any, error)
- func (storage *DatabaseSecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)
- func (storage *DatabaseSecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error
- func (storage *DatabaseSecondaryStorage) StartCleanup()
- func (storage *DatabaseSecondaryStorage) TTL(ctx context.Context, key string) (*time.Duration, error)
- type DatabaseStorageConfig
- type KeyValueStore
- type MemorySecondaryStorage
- func (storage *MemorySecondaryStorage) Close() error
- func (storage *MemorySecondaryStorage) Delete(ctx context.Context, key string) error
- func (storage *MemorySecondaryStorage) Get(ctx context.Context, key string) (any, error)
- func (storage *MemorySecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)
- func (storage *MemorySecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error
- func (storage *MemorySecondaryStorage) TTL(ctx context.Context, key string) (*time.Duration, error)
- type MemoryStorageConfig
- type RedisSecondaryStorage
- func (rs *RedisSecondaryStorage) Close() error
- func (rs *RedisSecondaryStorage) Delete(ctx context.Context, key string) error
- func (rs *RedisSecondaryStorage) Get(ctx context.Context, key string) (any, error)
- func (rs *RedisSecondaryStorage) Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)
- func (rs *RedisSecondaryStorage) Set(ctx context.Context, key string, value any, ttl *time.Duration) error
- func (rs *RedisSecondaryStorage) TTL(ctx context.Context, key string) (*time.Duration, error)
- type RedisSecondaryStorageOptions
- type RedisStorageConfig
- type SecondaryStorageAPI
- type SecondaryStoragePlugin
- func (p *SecondaryStoragePlugin) Close() error
- func (p *SecondaryStoragePlugin) Config() any
- func (p *SecondaryStoragePlugin) Init(ctx *models.PluginContext) error
- func (p *SecondaryStoragePlugin) Metadata() models.PluginMetadata
- func (p *SecondaryStoragePlugin) Migrations(ctx context.Context, dbProvider string) (*embed.FS, error)
- func (p *SecondaryStoragePlugin) OnConfigUpdate(config *models.Config) error
- type SecondaryStoragePluginConfig
- type SecondaryStorageProvider
- type SecondaryStorageServiceImpl
Constants ¶
This section is empty.
Variables ¶
var ( ErrRedisConfigURLNotProvided = errors.New("redis secondary storage configuration URL not provided") ErrDatabaseConfigNotProvided = errors.New("database secondary storage configuration not provided") )
Functions ¶
func GetMigrations ¶
GetMigrations returns the migrations for the specified database provider.
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 ¶
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.
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:",pk,type:varchar(255)"`
Value string `json:"value"`
ExpiresAt *time.Time `json:"expires_at" bun:"expires_at,nullzero"`
CreatedAt time.Time `json:"created_at" bun:"created_at,notnull,default:current_timestamp"`
UpdatedAt time.Time `json:"updated_at" bun:"updated_at,notnull,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 ¶
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.
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 ¶
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
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 ¶
func New(config SecondaryStoragePluginConfig) *SecondaryStoragePlugin
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) Init ¶
func (p *SecondaryStoragePlugin) Init(ctx *models.PluginContext) error
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 (p *SecondaryStoragePlugin) Metadata() models.PluginMetadata
func (*SecondaryStoragePlugin) Migrations ¶
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 ¶
func (s *SecondaryStorageServiceImpl) GetStorage() models.SecondaryStorage
GetStorage returns the configured SecondaryStorage backend