storage

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyKey = errors.New("empty key")

ErrEmptyKey is returned by Set/SetWithTTL when called with an empty key, which Redis treats as a no-op.

View Source
var ErrEmptyQueue = errors.New("empty queue")

ErrEmptyQueue signals that Dequeue had no item to return; callers use it to distinguish empty from error.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned by Storage implementations when a key does not exist or has expired.

Functions

func NewClient

func NewClient(host string) *redis.Client

NewClient creates a new Redis client connected to the given host.

func NewGCSClient added in v0.0.20

func NewGCSClient(ctx context.Context, credentialsFile, url string) (*gcs.Client, error)

NewGCSClient creates a new Google Cloud Storage client.

If credentialsFile is empty, Application Default Credentials are used. If url is non-empty, it is used as the endpoint without authentication — intended for emulators (e.g. fake-gcs-server) in tests.

Types

type GCSStorage added in v0.0.20

type GCSStorage[T any] struct {
	// contains filtered or unexported fields
}

GCSStorage is a Storage backed by a Google Cloud Storage bucket, holding one object per key under the given name prefix.

func NewGCSStorage added in v0.0.20

func NewGCSStorage[T any](client *gcs.Client, bucket, prefix string) *GCSStorage[T]

NewGCSStorage creates a new GCSStorage[T] storing objects as <prefix>/<key> in bucket.

func (*GCSStorage[T]) Get added in v0.0.20

func (s *GCSStorage[T]) Get(ctx context.Context, key string) (T, error)

func (*GCSStorage[T]) Remove added in v0.0.20

func (s *GCSStorage[T]) Remove(ctx context.Context, key string) error

func (*GCSStorage[T]) Set added in v0.0.20

func (s *GCSStorage[T]) Set(ctx context.Context, key string, item T) error

func (*GCSStorage[T]) SetWithTTL added in v0.0.20

func (s *GCSStorage[T]) SetWithTTL(ctx context.Context, key string, item T, expiration time.Duration) error

type Notifier

type Notifier interface {
	// Publish sends a notification on the given channel.
	Publish(ctx context.Context, channel string) error
	// Subscribe returns a Subscription for the given channel.
	Subscribe(ctx context.Context, channel string) Subscription
}

Notifier provides pub/sub functionality for key-based notifications.

func NewNotifier

func NewNotifier(client *redis.Client) Notifier

NewNotifier creates a new Notifier backed by Redis.

type Observer added in v0.0.19

type Observer interface {
	Observe(backend, namespace, operation, outcome string, d time.Duration)
}

Observer records the outcome and latency of a single storage operation. It is injected from the caller so this package stays free of a metrics dependency.

type Queue

type Queue[T any] interface {
	Enqueue(ctx context.Context, item T) error
	Dequeue(ctx context.Context) (T, error)
	QueueLength(ctx context.Context) (int64, error)
}

Queue is a generic FIFO queue backed by a persistent store.

func NewQueue

func NewQueue[T any](keyPrefix string, client *redis.Client) Queue[T]

NewQueue creates a new RedisStorage with the Redis client and storing key prefix that is used as a queue.

type RedisStorage

type RedisStorage[T any] struct {
	// contains filtered or unexported fields
}

RedisStorage is a storage backed by Redis. All storing keys are prefixed with keyPrefix-.

func NewRedisStorage

func NewRedisStorage[T any](keyPrefix string, client *redis.Client) *RedisStorage[T]

NewRedisStorage creates a new RedisStorage with the Redis client and storing key prefix.

func (*RedisStorage[T]) Clear

func (s *RedisStorage[T]) Clear(ctx context.Context) error

Clear deletes all entries under this storage's keyPrefix.

func (*RedisStorage[T]) Dequeue

func (s *RedisStorage[T]) Dequeue(ctx context.Context) (T, error)

Dequeue dequeues an item from the queue. If no item is available, ErrEmptyQueue error is returned.

func (*RedisStorage[T]) Enqueue

func (s *RedisStorage[T]) Enqueue(ctx context.Context, item T) error

Enqueue enqueues an item to the queue.

func (*RedisStorage[T]) Get

func (s *RedisStorage[T]) Get(ctx context.Context, key string) (T, error)

Get retrieves the value by the key.

func (*RedisStorage[T]) Publish

func (s *RedisStorage[T]) Publish(ctx context.Context, channel string) error

Publish sends a notification on the given channel.

func (*RedisStorage[T]) QueueLength

func (s *RedisStorage[T]) QueueLength(ctx context.Context) (int64, error)

func (*RedisStorage[T]) Remove

func (s *RedisStorage[T]) Remove(ctx context.Context, key string) error

Remove deletes the value stored for the key.

func (*RedisStorage[T]) Set

func (s *RedisStorage[T]) Set(ctx context.Context, key string, item T) error

Set stores the item with the key without expiration.

func (*RedisStorage[T]) SetWithTTL

func (s *RedisStorage[T]) SetWithTTL(ctx context.Context, key string, item T, expiration time.Duration) error

SetWithTTL stores the item with the key and expiration.

func (*RedisStorage[T]) Subscribe

func (s *RedisStorage[T]) Subscribe(ctx context.Context, channel string) Subscription

Subscribe returns a Subscription for the given channel.

type Storage

type Storage[T any] interface {
	Set(ctx context.Context, key string, item T) error
	SetWithTTL(ctx context.Context, key string, item T, expiration time.Duration) error
	Get(ctx context.Context, key string) (T, error)
	Remove(ctx context.Context, key string) error
}

Storage is a generic key-value store.

func WithMetrics added in v0.0.19

func WithMetrics[T any](s Storage[T], obs Observer, backend, namespace string) Storage[T]

WithMetrics wraps s so each operation reports to obs under the given backend and namespace. It returns s unchanged when obs is nil, so a disabled observer adds no overhead to the hot path.

type Subscription

type Subscription interface {
	// ReceiveMessage blocks until a message arrives or the context is cancelled.
	ReceiveMessage(ctx context.Context) (string, error)
	// Close closes the subscription.
	Close() error
}

Subscription represents an active pub/sub subscription.

Jump to

Keyboard shortcuts

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