kv

package
v0.0.0-...-8f28ad5 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	SetWithTTL(ctx context.Context, key, value string, ttl time.Duration) error
	SetWithExpireAt(ctx context.Context, key, value string, expireAt time.Time) error
	Get(ctx context.Context, key string) (*KvItem, error)
	Exists(ctx context.Context, key string) (bool, error)
	Del(ctx context.Context, key string) error
}

Interface is a kv interface

type Kv

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

Kv is a key-value store for postgres

func NewKv

func NewKv(db **gorm.DB, opts ...Option) (*Kv, error)

NewKv create a new kv

func (*Kv) Del

func (kv *Kv) Del(ctx context.Context, key string) error

Del removes the key from the store.

func (*Kv) Exists

func (kv *Kv) Exists(ctx context.Context, key string) (bool, error)

Exists checks whether a key exists and hasn't expired.

func (*Kv) Get

func (kv *Kv) Get(ctx context.Context, key string) (*KvItem, error)

Get retrieves the key's document. If the key is expired, it deletes the record and returns an error.

func (*Kv) SetWithExpireAt

func (kv *Kv) SetWithExpireAt(ctx context.Context, key, value string, expireAt time.Time) error

SetWithExpireAt stores the key-value pair with a specific expiration time. Uses GORM's OnConflict clause to avoid SQL injection risks.

func (*Kv) SetWithTTL

func (kv *Kv) SetWithTTL(ctx context.Context, key, value string, ttl time.Duration) error

SetWithTTL stores the key-value pair with a time-to-live duration.

type KvItem

type KvItem struct {
	Key       string    `gorm:"column:key;primaryKey" json:"key"`
	Value     string    `gorm:"column:value" json:"value"`
	CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
	ExpireAt  time.Time `gorm:"column:expire_at" json:"expire_at"`
}

KvItem is a kv doc

type Option

type Option func(*option) error

Option is a function that configures the kv

func WithDBName

func WithDBName(tableName string) Option

WithDBName is a option to set db name

Jump to

Keyboard shortcuts

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