storage

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

README

Storage Package

This package provides the storage abstraction layer for Starport, supporting multiple backend implementations with a unified interface.

Overview

The storage package defines a KVStore interface that abstracts key-value storage operations. It supports:

  • Basic CRUD operations (Get, Set, Delete, Exists)
  • TTL (Time-To-Live) support for temporary data
  • Atomic operations (Increment, Decrement, CompareAndSwap)
  • Batch operations for efficiency
  • Transaction support for atomic multi-operation updates
  • Scanning/listing capabilities

Architecture

storage/
├── interface.go      # KVStore interface and configuration types
├── open.go          # Open function for creating storage instances
├── serialization.go  # Helpers for data serialization
├── mock.go          # In-memory mock implementation for testing
└── README.md        # This file

Storage Backends

Badger (Default)
  • Embedded key-value store
  • Zero external dependencies
  • Excellent performance (<1ms latency)
  • Perfect for single-node deployments
Valkey
  • Redis-compatible distributed store
  • Required for multi-node deployments
  • Supports shared state across instances
  • Higher latency (<5ms) but better scalability

Usage

// Create storage instance
config := storage.Config{
    Type: "badger",
    Badger: storage.BadgerConfig{
        Path: "./data/badger",
    },
}

store, err := storage.Open(config)
if err != nil {
    return err
}
defer store.Close()

// Basic operations
ctx := context.Background()
err = store.Set(ctx, "key", []byte("value"))
value, err := store.Get(ctx, "key")

// TTL operations
err = store.SetWithTTL(ctx, "temp-key", []byte("temp-value"), 5*time.Minute)

// Atomic operations
count, err := store.Increment(ctx, "counter", 1)

// Transactions
tx, err := store.BeginTransaction(ctx)
tx.Set("key1", []byte("value1"))
tx.Set("key2", []byte("value2"))
err = tx.Commit(ctx)

Testing

The package includes a comprehensive mock implementation (MockStore) that implements the full KVStore interface with in-memory storage. This is perfect for unit testing without external dependencies.

// Create mock store for testing
store := storage.NewMockStore()
defer store.Close()

// Use exactly like a real store
err := store.Set(ctx, "test-key", []byte("test-value"))

Key Patterns

The storage layer uses consistent key patterns for different data types:

  • API Keys: apikey:{hash}
  • Presets: preset:{name}
  • BYOK Credentials: credential:{api_key_id}:{provider}
  • Filters: filter:{name}
  • Rate Limits: ratelimit:{key}:{window}

Performance Considerations

  1. Batch Operations: Use batch operations when working with multiple keys to reduce round trips
  2. TTL Usage: Use TTL for temporary data to avoid manual cleanup
  3. Transaction Scope: Keep transactions small and focused
  4. Key Design: Use hierarchical key patterns for efficient scanning

Error Handling

The package defines specific error types:

  • ErrNotFound: Key does not exist
  • ErrConflict: Write conflict (e.g., CAS mismatch)
  • ErrInvalidKey: Invalid key format
  • ErrStorageClosed: Storage instance is closed
  • ErrTimeout: Operation timed out

Always check for ErrNotFound when a key might not exist:

value, err := store.Get(ctx, "maybe-missing")
if errors.Is(err, storage.ErrNotFound) {
    // Handle missing key
}

Documentation

Overview

Package storage provides a key-value storage abstraction layer with support for multiple backend implementations including embedded and distributed stores.

Package storage provides a key-value storage abstraction layer with support for multiple backend implementations including embedded and distributed stores.

Index

Constants

View Source
const (
	// StorageTypeBadger represents the Badger embedded storage backend
	StorageTypeBadger = "badger"
	// StorageTypeValkey represents the Valkey distributed storage backend
	StorageTypeValkey = "valkey"
)

Storage backend type constants

View Source
const (
	// KeyPrefixResponse is the prefix for cached responses
	KeyPrefixResponse = "response:"

	// KeyPrefixModel is the prefix for model metadata
	KeyPrefixModel = "model:"
)

Key prefixes for different data types

Variables

View Source
var (
	// ErrTransactionClosed is returned when operations are attempted on a closed transaction
	ErrTransactionClosed = errors.New("transaction is closed")
	// ErrTransactionCommitted is returned when operations are attempted on a committed transaction
	ErrTransactionCommitted = errors.New("transaction already committed")
	// ErrTransactionRolledBack is returned when operations are attempted on a rolled back transaction
	ErrTransactionRolledBack = errors.New("transaction already rolled back")

	// PubSub-related errors
	// ErrPubSubClosed is returned when operations are attempted on a closed PubSub client
	ErrPubSubClosed = errors.New("pubsub client is closed")
)

Transaction-related errors

View Source
var (
	// ErrNotFound is returned when a key does not exist
	ErrNotFound = errors.New("key not found")
	// ErrConflict is returned when a write conflict occurs (e.g., CAS mismatch)
	ErrConflict = errors.New("write conflict")
	// ErrInvalidKey is returned when an invalid key is provided
	ErrInvalidKey = errors.New("invalid key")
	// ErrInvalidMutation is returned when an atomic mutation set is malformed.
	ErrInvalidMutation = errors.New("invalid compare-and-swap mutation")
	// ErrStorageClosed is returned when operations are attempted on a closed store
	ErrStorageClosed = errors.New("storage closed")
	// ErrTimeout is returned when an operation times out
	ErrTimeout = errors.New("operation timeout")
)

Common errors returned by storage operations

Functions

func Deserialize

func Deserialize(data []byte, v any) error

Deserialize converts bytes to the specified type using JSON decoding

func DeserializeInt64

func DeserializeInt64(data []byte) (int64, error)

DeserializeInt64 converts bytes to int64

func DeserializeString

func DeserializeString(data []byte) string

DeserializeString converts bytes to string

func ModelKey

func ModelKey(modelID string) string

ModelKey generates a storage key for model metadata

func ResponseKey

func ResponseKey(cacheKey string) string

ResponseKey generates a storage key for cached responses

func Serialize

func Serialize(v any) ([]byte, error)

Serialize converts any value to bytes using JSON encoding

func SerializeInt64

func SerializeInt64(n int64) []byte

SerializeInt64 converts an int64 to bytes

func SerializeString

func SerializeString(s string) []byte

SerializeString converts a string to bytes

Types

type BadgerConfig

type BadgerConfig struct {
	Path         string `env:"PATH,default=./data/badger"`
	SyncWrites   bool   `env:"SYNC_WRITES,default=false"`
	Compression  bool   `env:"COMPRESSION,default=true"`
	NumVersions  int    `env:"NUM_VERSIONS,default=1"`
	NumLevelZero int    `env:"NUM_LEVEL_ZERO,default=5"`
	MemTableSize int64  `env:"MEM_TABLE_SIZE,default=67108864"` // 64MB
}

BadgerConfig represents Badger-specific configuration

type BadgerStore

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

BadgerStore implements the KVStore interface using Badger DB

func OpenBadger

func OpenBadger(config BadgerConfig) (*BadgerStore, error)

OpenBadger creates a new BadgerStore instance with the given configuration

func (*BadgerStore) Backup

func (s *BadgerStore) Backup(_ context.Context, path string) error

Backup creates a backup of the database

func (*BadgerStore) BatchDelete

func (s *BadgerStore) BatchDelete(_ context.Context, keys []string) error

BatchDelete removes multiple keys

func (*BadgerStore) BatchGet

func (s *BadgerStore) BatchGet(_ context.Context, keys []string) (map[string][]byte, error)

BatchGet retrieves multiple values by keys

func (*BadgerStore) BatchSet

func (s *BadgerStore) BatchSet(_ context.Context, items map[string][]byte) error

BatchSet stores multiple key-value pairs

func (*BadgerStore) BatchSetWithTTL

func (s *BadgerStore) BatchSetWithTTL(_ context.Context, items map[string][]byte, ttl time.Duration) error

BatchSetWithTTL stores multiple key-value pairs with TTL

func (*BadgerStore) BeginTransaction

func (s *BadgerStore) BeginTransaction(_ context.Context) (Transaction, error)

BeginTransaction starts a new transaction

func (*BadgerStore) Close

func (s *BadgerStore) Close() error

Close closes the store

func (*BadgerStore) CompareAndSwap

func (s *BadgerStore) CompareAndSwap(ctx context.Context, key string, old, newValue []byte) error

CompareAndSwap atomically updates a value if it matches the expected value

func (*BadgerStore) CompareAndSwapBatch

func (s *BadgerStore) CompareAndSwapBatch(_ context.Context, mutations []CompareAndSwapMutation) error

CompareAndSwapBatch applies all conditional writes or none of them.

func (*BadgerStore) Decrement

func (s *BadgerStore) Decrement(ctx context.Context, key string, delta int64) (int64, error)

Decrement atomically decrements a counter

func (*BadgerStore) Delete

func (s *BadgerStore) Delete(_ context.Context, key string) error

Delete removes a key

func (*BadgerStore) Exists

func (s *BadgerStore) Exists(_ context.Context, key string) (bool, error)

Exists checks if a key exists

func (*BadgerStore) ExpireAt

func (s *BadgerStore) ExpireAt(ctx context.Context, key string, expireAt time.Time) error

ExpireAt sets a key to expire at a specific time

func (*BadgerStore) Get

func (s *BadgerStore) Get(_ context.Context, key string) ([]byte, error)

Get retrieves a value by key

func (*BadgerStore) GetTTL

func (s *BadgerStore) GetTTL(_ context.Context, key string) (time.Duration, error)

GetTTL returns the TTL for a key

func (*BadgerStore) Increment

func (s *BadgerStore) Increment(_ context.Context, key string, delta int64) (int64, error)

Increment atomically increments a counter

func (*BadgerStore) Ping

func (s *BadgerStore) Ping(ctx context.Context) error

Ping checks if the store is healthy

func (*BadgerStore) Restore

func (s *BadgerStore) Restore(_ context.Context, path string) error

Restore restores the database from a backup

func (*BadgerStore) Scan

func (s *BadgerStore) Scan(_ context.Context, pattern string, limit int) ([]string, error)

Scan returns keys matching a pattern

func (*BadgerStore) ScanWithPrefix

func (s *BadgerStore) ScanWithPrefix(_ context.Context, prefix string, limit int) ([]string, error)

ScanWithPrefix returns keys with a specific prefix

func (*BadgerStore) Set

func (s *BadgerStore) Set(_ context.Context, key string, value []byte) error

Set stores a key-value pair

func (*BadgerStore) SetWithTTL

func (s *BadgerStore) SetWithTTL(_ context.Context, key string, value []byte, ttl time.Duration) error

SetWithTTL stores a key-value pair with a TTL Note: Badger v4 has per-second TTL granularity. TTLs less than 1 second may expire immediately.

type BadgerTransaction

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

BadgerTransaction represents a Badger transaction

func (*BadgerTransaction) Commit

func (t *BadgerTransaction) Commit(_ context.Context) error

Commit commits the transaction

func (*BadgerTransaction) CompareAndSwap

func (t *BadgerTransaction) CompareAndSwap(key string, old, newValue []byte) error

CompareAndSwap atomically updates a value if it matches the expected value

func (*BadgerTransaction) Delete

func (t *BadgerTransaction) Delete(key string) error

Delete removes a key within the transaction

func (*BadgerTransaction) Get

func (t *BadgerTransaction) Get(key string) ([]byte, error)

Get retrieves a value within the transaction

func (*BadgerTransaction) Increment

func (t *BadgerTransaction) Increment(key string, delta int64) (int64, error)

Increment atomically increments a counter within the transaction

func (*BadgerTransaction) Rollback

func (t *BadgerTransaction) Rollback() error

Rollback aborts the transaction

func (*BadgerTransaction) Set

func (t *BadgerTransaction) Set(key string, value []byte) error

Set stores a key-value pair within the transaction

func (*BadgerTransaction) SetWithTTL

func (t *BadgerTransaction) SetWithTTL(key string, value []byte, ttl time.Duration) error

SetWithTTL stores a key-value pair with TTL within the transaction

type CompareAndSwapMutation

type CompareAndSwapMutation struct {
	Key           string
	ExpectedValue []byte
	NewValue      []byte
	TTL           time.Duration
}

CompareAndSwapMutation is one conditional write in an atomic mutation set. A nil ExpectedValue requires absence. A nil NewValue deletes the key. A positive TTL replaces the key expiration; zero preserves an existing TTL.

type Config

type Config struct {
	Type   string       `env:"TYPE,default=badger"`
	Badger BadgerConfig `env:",prefix=BADGER_"`
	Valkey ValkeyConfig `env:",prefix=VALKEY_"`
}

Config represents storage configuration

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the storage configuration

type KVStore

type KVStore interface {
	// Basic operations
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte) error
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) (bool, error)

	// TTL operations
	SetWithTTL(ctx context.Context, key string, value []byte, ttl time.Duration) error
	GetTTL(ctx context.Context, key string) (time.Duration, error)
	ExpireAt(ctx context.Context, key string, expireAt time.Time) error

	// Atomic operations
	Increment(ctx context.Context, key string, delta int64) (int64, error)
	Decrement(ctx context.Context, key string, delta int64) (int64, error)
	CompareAndSwap(ctx context.Context, key string, old, newValue []byte) error
	CompareAndSwapBatch(ctx context.Context, mutations []CompareAndSwapMutation) error

	// Batch operations
	BatchGet(ctx context.Context, keys []string) (map[string][]byte, error)
	BatchSet(ctx context.Context, items map[string][]byte) error
	BatchDelete(ctx context.Context, keys []string) error
	BatchSetWithTTL(ctx context.Context, items map[string][]byte, ttl time.Duration) error

	// Transaction support
	BeginTransaction(ctx context.Context) (Transaction, error)

	// Scan operations for listing keys
	Scan(ctx context.Context, pattern string, limit int) ([]string, error)
	ScanWithPrefix(ctx context.Context, prefix string, limit int) ([]string, error)

	// Health check and lifecycle
	Ping(ctx context.Context) error
	Close() error
}

KVStore defines the interface for key-value storage operations. All implementations must be thread-safe and support concurrent access.

func Open

func Open(config Config) (KVStore, error)

Open creates a new KVStore instance based on the configuration. This follows the Go convention of using Open for creating connections.

func OpenValkey

func OpenValkey(config ValkeyConfig) (KVStore, error)

OpenValkey creates a new Valkey-backed KVStore

type MockStore

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

MockStore is an in-memory implementation of KVStore for testing

func NewMockStore

func NewMockStore() *MockStore

NewMockStore creates a new mock KVStore for testing

func (*MockStore) BatchDelete

func (m *MockStore) BatchDelete(ctx context.Context, keys []string) error

BatchDelete removes multiple keys

func (*MockStore) BatchGet

func (m *MockStore) BatchGet(ctx context.Context, keys []string) (map[string][]byte, error)

BatchGet retrieves multiple values

func (*MockStore) BatchSet

func (m *MockStore) BatchSet(ctx context.Context, items map[string][]byte) error

BatchSet stores multiple values

func (*MockStore) BatchSetWithTTL

func (m *MockStore) BatchSetWithTTL(ctx context.Context, items map[string][]byte, ttl time.Duration) error

BatchSetWithTTL stores multiple values with TTL

func (*MockStore) BeginTransaction

func (m *MockStore) BeginTransaction(ctx context.Context) (Transaction, error)

BeginTransaction starts a new transaction

func (*MockStore) Close

func (m *MockStore) Close() error

Close closes the store

func (*MockStore) CompareAndSwap

func (m *MockStore) CompareAndSwap(ctx context.Context, key string, old, newValue []byte) error

CompareAndSwap atomically updates a value if it matches the expected value

func (*MockStore) CompareAndSwapBatch

func (m *MockStore) CompareAndSwapBatch(ctx context.Context, mutations []CompareAndSwapMutation) error

CompareAndSwapBatch applies all conditional writes or none of them.

func (*MockStore) Decrement

func (m *MockStore) Decrement(ctx context.Context, key string, delta int64) (int64, error)

Decrement atomically decrements a counter

func (*MockStore) Delete

func (m *MockStore) Delete(ctx context.Context, key string) error

Delete removes a key

func (*MockStore) Exists

func (m *MockStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists

func (*MockStore) ExpireAt

func (m *MockStore) ExpireAt(ctx context.Context, key string, expireAt time.Time) error

ExpireAt sets an absolute expiration time for a key

func (*MockStore) Get

func (m *MockStore) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value by key

func (*MockStore) GetTTL

func (m *MockStore) GetTTL(ctx context.Context, key string) (time.Duration, error)

GetTTL returns the remaining TTL for a key

func (*MockStore) Increment

func (m *MockStore) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment atomically increments a counter

func (*MockStore) Ping

func (m *MockStore) Ping(ctx context.Context) error

Ping checks if the store is healthy

func (*MockStore) Scan

func (m *MockStore) Scan(ctx context.Context, pattern string, limit int) ([]string, error)

Scan returns keys matching a pattern

func (*MockStore) ScanWithPrefix

func (m *MockStore) ScanWithPrefix(ctx context.Context, prefix string, limit int) ([]string, error)

ScanWithPrefix returns keys with a specific prefix

func (*MockStore) Set

func (m *MockStore) Set(ctx context.Context, key string, value []byte) error

Set stores a value by key

func (*MockStore) SetWithTTL

func (m *MockStore) SetWithTTL(ctx context.Context, key string, value []byte, ttl time.Duration) error

SetWithTTL stores a value with expiration

type MockTransaction

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

MockTransaction represents a mock transaction

func (*MockTransaction) Commit

func (t *MockTransaction) Commit(_ context.Context) error

Commit applies all pending operations

func (*MockTransaction) CompareAndSwap

func (t *MockTransaction) CompareAndSwap(key string, old, newValue []byte) error

CompareAndSwap performs CAS within the transaction

func (*MockTransaction) Delete

func (t *MockTransaction) Delete(key string) error

Delete removes a key within the transaction

func (*MockTransaction) Get

func (t *MockTransaction) Get(key string) ([]byte, error)

Get retrieves a value within the transaction

func (*MockTransaction) Increment

func (t *MockTransaction) Increment(key string, delta int64) (int64, error)

Increment atomically increments within the transaction

func (*MockTransaction) Rollback

func (t *MockTransaction) Rollback() error

Rollback discards all pending operations

func (*MockTransaction) Set

func (t *MockTransaction) Set(key string, value []byte) error

Set stores a value within the transaction

func (*MockTransaction) SetWithTTL

func (t *MockTransaction) SetWithTTL(key string, value []byte, ttl time.Duration) error

SetWithTTL stores a value with TTL within the transaction

type PubSubClient

type PubSubClient interface {
	// Subscribe to a pattern and handle messages
	Subscribe(pattern string, handler func(channel, message string)) error
	// Publish a message to a channel
	Publish(ctx context.Context, channel string, message string) error
	// Close the pub/sub client
	Close() error
}

PubSubClient defines the interface for pub/sub operations used for cache invalidation

type PubSubProvider

type PubSubProvider interface {
	GetPubSub() PubSubClient
}

PubSubProvider is implemented by storage backends that support pub/sub

type Transaction

type Transaction interface {
	// Basic operations within transaction
	Get(key string) ([]byte, error)
	Set(key string, value []byte) error
	Delete(key string) error

	// TTL operations within transaction
	SetWithTTL(key string, value []byte, ttl time.Duration) error

	// Atomic operations within transaction
	Increment(key string, delta int64) (int64, error)
	CompareAndSwap(key string, old, newValue []byte) error

	// Transaction control
	Commit(ctx context.Context) error
	Rollback() error
}

Transaction represents an atomic set of operations

type ValkeyConfig

type ValkeyConfig struct {
	URL          string        `env:"URL,default=redis://localhost:6379"`
	MaxRetries   int           `env:"MAX_RETRIES,default=3"`
	MinIdleConns int           `env:"MIN_IDLE_CONNS,default=10"`
	MaxConnAge   time.Duration `env:"MAX_CONN_AGE,default=0"`
	PoolTimeout  time.Duration `env:"POOL_TIMEOUT,default=4s"`
	ReadTimeout  time.Duration `env:"READ_TIMEOUT,default=3s"`
	WriteTimeout time.Duration `env:"WRITE_TIMEOUT,default=3s"`
	Password     string        `env:"PASSWORD"`
	DB           int           `env:"DB,default=0"`
	ClusterMode  bool          `env:"CLUSTER_MODE,default=false"`
}

ValkeyConfig represents Valkey/Redis-specific configuration

type ValkeyPubSub

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

ValkeyPubSub implements PubSubClient using Valkey pub/sub

func NewValkeyPubSub

func NewValkeyPubSub(client valkey.Client) *ValkeyPubSub

NewValkeyPubSub creates a new Valkey pub/sub client

func (*ValkeyPubSub) Close

func (v *ValkeyPubSub) Close() error

Close closes all subscriptions

func (*ValkeyPubSub) Publish

func (v *ValkeyPubSub) Publish(ctx context.Context, channel string, message string) error

Publish publishes a message to a channel

func (*ValkeyPubSub) Subscribe

func (v *ValkeyPubSub) Subscribe(pattern string, handler func(channel, message string)) error

Subscribe subscribes to a pattern and handles messages

type ValkeyStore

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

ValkeyStore implements KVStore interface using Valkey

func (*ValkeyStore) BatchDelete

func (v *ValkeyStore) BatchDelete(ctx context.Context, keys []string) error

BatchDelete removes multiple keys

func (*ValkeyStore) BatchGet

func (v *ValkeyStore) BatchGet(ctx context.Context, keys []string) (map[string][]byte, error)

BatchGet retrieves multiple values

func (*ValkeyStore) BatchSet

func (v *ValkeyStore) BatchSet(ctx context.Context, items map[string][]byte) error

BatchSet stores multiple key-value pairs

func (*ValkeyStore) BatchSetWithTTL

func (v *ValkeyStore) BatchSetWithTTL(ctx context.Context, items map[string][]byte, ttl time.Duration) error

BatchSetWithTTL stores multiple key-value pairs with TTL

func (*ValkeyStore) BeginTransaction

func (v *ValkeyStore) BeginTransaction(ctx context.Context) (Transaction, error)

BeginTransaction starts a new transaction

func (*ValkeyStore) Close

func (v *ValkeyStore) Close() error

Close closes the connection

func (*ValkeyStore) CompareAndSwap

func (v *ValkeyStore) CompareAndSwap(ctx context.Context, key string, old, newValue []byte) error

CompareAndSwap atomically updates a value if it matches the old value

func (*ValkeyStore) CompareAndSwapBatch

func (v *ValkeyStore) CompareAndSwapBatch(ctx context.Context, mutations []CompareAndSwapMutation) error

CompareAndSwapBatch applies all conditional writes or none of them.

func (*ValkeyStore) Decrement

func (v *ValkeyStore) Decrement(ctx context.Context, key string, delta int64) (int64, error)

Decrement atomically decrements a value

func (*ValkeyStore) Delete

func (v *ValkeyStore) Delete(ctx context.Context, key string) error

Delete removes a key

func (*ValkeyStore) Exists

func (v *ValkeyStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists

func (*ValkeyStore) ExpireAt

func (v *ValkeyStore) ExpireAt(ctx context.Context, key string, expireAt time.Time) error

ExpireAt sets expiration time for a key

func (*ValkeyStore) Get

func (v *ValkeyStore) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value by key

func (*ValkeyStore) GetPubSub

func (v *ValkeyStore) GetPubSub() PubSubClient

GetPubSub returns the pub/sub client for cache invalidation

func (*ValkeyStore) GetTTL

func (v *ValkeyStore) GetTTL(ctx context.Context, key string) (time.Duration, error)

GetTTL returns the remaining TTL for a key

func (*ValkeyStore) Increment

func (v *ValkeyStore) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment atomically increments a value

func (*ValkeyStore) Ping

func (v *ValkeyStore) Ping(ctx context.Context) error

Ping checks if the connection is alive

func (*ValkeyStore) Scan

func (v *ValkeyStore) Scan(ctx context.Context, pattern string, limit int) ([]string, error)

Scan returns keys matching a pattern

func (*ValkeyStore) ScanWithPrefix

func (v *ValkeyStore) ScanWithPrefix(ctx context.Context, prefix string, limit int) ([]string, error)

ScanWithPrefix returns keys with a specific prefix

func (*ValkeyStore) Set

func (v *ValkeyStore) Set(ctx context.Context, key string, value []byte) error

Set stores a value with a key

func (*ValkeyStore) SetWithTTL

func (v *ValkeyStore) SetWithTTL(ctx context.Context, key string, value []byte, ttl time.Duration) error

SetWithTTL stores a value with expiration

type ValkeyTransaction

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

ValkeyTransaction implements Transaction interface

func (*ValkeyTransaction) Commit

func (t *ValkeyTransaction) Commit(ctx context.Context) error

Commit executes all commands in the transaction

func (*ValkeyTransaction) CompareAndSwap

func (t *ValkeyTransaction) CompareAndSwap(key string, _, newValue []byte) error

CompareAndSwap within the transaction

func (*ValkeyTransaction) Delete

func (t *ValkeyTransaction) Delete(key string) error

Delete removes a key within the transaction

func (*ValkeyTransaction) Get

func (t *ValkeyTransaction) Get(key string) ([]byte, error)

Get retrieves a value within the transaction

func (*ValkeyTransaction) Increment

func (t *ValkeyTransaction) Increment(key string, delta int64) (int64, error)

Increment atomically increments within the transaction

func (*ValkeyTransaction) Rollback

func (t *ValkeyTransaction) Rollback() error

Rollback discards the transaction

func (*ValkeyTransaction) Set

func (t *ValkeyTransaction) Set(key string, value []byte) error

Set stores a value within the transaction

func (*ValkeyTransaction) SetWithTTL

func (t *ValkeyTransaction) SetWithTTL(key string, value []byte, ttl time.Duration) error

SetWithTTL stores a value with TTL within the transaction

Jump to

Keyboard shortcuts

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