storage

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyKey = errors.New("empty key")
View Source
var ErrEmptyQueue = errors.New("empty queue")
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 NewFirestoreClient

func NewFirestoreClient(ctx context.Context, projectID, databaseID, credentialsFile, url string) (*firestore.Client, error)

NewFirestoreClient creates a new Firestore client for the given project.

If credentialsFile is empty, Application Default Credentials are used. If databaseID is empty, the default database is used.

Types

type FirestoreStorage

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

FirestoreStorage is a Storage backed by Firestore.

func NewFirestoreStorage

func NewFirestoreStorage[T any](client *firestore.Client, collection string) *FirestoreStorage[T]

NewFirestoreStorage creates a new FirestoreStorage[T] backed by the given Firestore collection.

func (*FirestoreStorage[T]) Get

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

func (*FirestoreStorage[T]) Remove

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

func (*FirestoreStorage[T]) Set

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

func (*FirestoreStorage[T]) SetWithTTL

func (f *FirestoreStorage[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 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 storage's entries from database.

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]) GetWithTTL

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

GetWithTTL retrieves the value and its remaining expiration duration by the key.

func (*RedisStorage[T]) Ping

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

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.

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