45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package key_value
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"universe.mrlokop.dev/core/errors"
|
|
)
|
|
|
|
var (
|
|
ErrNoSuchKey = errors.Type("ErrNoSuchKey", "no such key")
|
|
)
|
|
|
|
type Capability uint8
|
|
|
|
const (
|
|
CapabilityTTL Capability = iota
|
|
)
|
|
|
|
type KeyValueProvider interface {
|
|
HasCapability(capability Capability) bool
|
|
|
|
ProvideBinary(prefix string) (KeyValueStoreBinary, error)
|
|
ProvideString(prefix string) (KeyValueStoreString, error)
|
|
}
|
|
|
|
type KeyValueStore[T any] interface {
|
|
HasCapability(capability Capability) bool
|
|
|
|
Get(ctx context.Context, key string) (T, error)
|
|
Has(ctx context.Context, key string) (bool, error)
|
|
|
|
Set(ctx context.Context, key string, value T) error
|
|
SetWithExpiration(ctx context.Context, key string, value T, expiresAt time.Time) error
|
|
|
|
SetIfNotExist(ctx context.Context, key string, value T) (bool, error)
|
|
SetIfNotExistWithExpiration(ctx context.Context, key string, value T, expiration time.Time) (bool, error)
|
|
|
|
ExpireAt(ctx context.Context, key string, expiration time.Time) error
|
|
|
|
Remove(ctx context.Context, key string) error
|
|
}
|
|
|
|
type KeyValueStoreBinary KeyValueStore[[]byte]
|
|
type KeyValueStoreString KeyValueStore[string]
|