rstring

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package rstring is a database-backed string repository. It provides methods to interact with strings in the database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sqlx.DB[*Tx]
}

DB is a database-backed string repository. A string is a slice of bytes associated with a key. Use the string repository to work with individual strings.

func New

func New(rw *sql.DB, ro *sql.DB) *DB

New connects to the string repository. Does not create the database schema.

func (*DB) Get

func (d *DB) Get(key string) (core.Value, error)

Get returns the value of the key. If the key does not exist or is not a string, returns ErrNotFound.

func (*DB) GetMany

func (d *DB) GetMany(keys ...string) (map[string]core.Value, error)

GetMany returns a map of values for given keys. Ignores keys that do not exist or not strings, and does not return them in the map.

func (*DB) Incr

func (d *DB) Incr(key string, delta int) (int, error)

Incr increments the integer key value by the specified amount. Returns the value after the increment. If the key does not exist, sets it to 0 before the increment. If the key value is not an integer, returns ErrValueType. If the key exists but is not a string, returns ErrKeyType.

func (*DB) IncrFloat

func (d *DB) IncrFloat(key string, delta float64) (float64, error)

IncrFloat increments the float key value by the specified amount. Returns the value after the increment. If the key does not exist, sets it to 0 before the increment. If the key value is not an float, returns ErrValueType. If the key exists but is not a string, returns ErrKeyType.

func (*DB) Set

func (d *DB) Set(key string, value any) error

Set sets the key value that will not expire. Overwrites the value if the key already exists. If the key exists but is not a string, returns ErrKeyType.

func (*DB) SetExpires

func (d *DB) SetExpires(key string, value any, ttl time.Duration) error

SetExpires sets the key value with an optional expiration time (if ttl > 0). Overwrites the value and ttl if the key already exists. If the key exists but is not a string, returns ErrKeyType.

func (*DB) SetMany

func (d *DB) SetMany(items map[string]any) error

SetMany sets the values of multiple keys. Overwrites values for keys that already exist and creates new keys/values for keys that do not exist. Removes the TTL for existing keys. If any of the keys exists but is not a string, returns ErrKeyType.

func (*DB) SetWith added in v0.3.0

func (d *DB) SetWith(key string, value any) SetCmd

SetWith sets the key value with additional options.

type SetCmd added in v0.3.0

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

SetCmd sets the key value.

func (SetCmd) At added in v0.3.0

func (c SetCmd) At(at time.Time) SetCmd

At sets the expiration time for the value.

func (SetCmd) IfExists added in v0.3.0

func (c SetCmd) IfExists() SetCmd

IfExists instructs to set the value only if the key exists.

func (SetCmd) IfNotExists added in v0.3.0

func (c SetCmd) IfNotExists() SetCmd

IfNotExists instructs to set the value only if the key does not exist.

func (SetCmd) KeepTTL added in v0.3.0

func (c SetCmd) KeepTTL() SetCmd

KeepTTL instructs to keep the expiration time already set for the key.

func (SetCmd) Run added in v0.3.0

func (c SetCmd) Run() (out SetOut, err error)

Run sets the key value according to the configured options. Returns the previous value (if any) and the operation result (if the key was created or updated).

Expiration time handling:

  • If called with TTL() > 0 or At(), sets the expiration time.
  • If called with KeepTTL(), keeps the expiration time already set for the key.
  • If called without TTL(), At() or KeepTTL(), sets the value that will not expire.

Existence checks:

  • If called with IfExists(), sets the value only if the key exists.
  • If called with IfNotExists(), sets the value only if the key does not exist.

If the key exists but is not a string, returns ErrKeyType (unless called with IfExists(), in which case does nothing).

func (SetCmd) TTL added in v0.3.0

func (c SetCmd) TTL(ttl time.Duration) SetCmd

TTL sets the time-to-live for the value.

type SetOut added in v0.3.0

type SetOut struct {
	Prev    core.Value
	Created bool
	Updated bool
}

SetOut is the output of the Set command.

type Tx

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

Tx is a string repository transaction.

func NewTx

func NewTx(tx sqlx.Tx) *Tx

NewTx creates a string repository transaction from a generic database transaction.

func (*Tx) Get

func (tx *Tx) Get(key string) (core.Value, error)

Get returns the value of the key. If the key does not exist or is not a string, returns ErrNotFound.

func (*Tx) GetMany

func (tx *Tx) GetMany(keys ...string) (map[string]core.Value, error)

GetMany returns a map of values for given keys. Ignores keys that do not exist or not strings, and does not return them in the map.

func (*Tx) Incr

func (tx *Tx) Incr(key string, delta int) (int, error)

Incr increments the integer key value by the specified amount. Returns the value after the increment. If the key does not exist, sets it to 0 before the increment. If the key value is not an integer, returns ErrValueType. If the key exists but is not a string, returns ErrKeyType.

func (*Tx) IncrFloat

func (tx *Tx) IncrFloat(key string, delta float64) (float64, error)

IncrFloat increments the float key value by the specified amount. Returns the value after the increment. If the key does not exist, sets it to 0 before the increment. If the key value is not an float, returns ErrValueType. If the key exists but is not a string, returns ErrKeyType.

func (*Tx) Set

func (tx *Tx) Set(key string, value any) error

Set sets the key value that will not expire. Overwrites the value if the key already exists. If the key exists but is not a string, returns ErrKeyType.

func (*Tx) SetExpires

func (tx *Tx) SetExpires(key string, value any, ttl time.Duration) error

SetExpires sets the key value with an optional expiration time (if ttl > 0). Overwrites the value and ttl if the key already exists. If the key exists but is not a string, returns ErrKeyType.

func (*Tx) SetMany

func (tx *Tx) SetMany(items map[string]any) error

SetMany sets the values of multiple keys. Overwrites values for keys that already exist and creates new keys/values for keys that do not exist. Removes the TTL for existing keys. If any of the keys exists but is not a string, returns ErrKeyType.

func (*Tx) SetWith added in v0.3.0

func (tx *Tx) SetWith(key string, value any) SetCmd

SetWith sets the key value with additional options.

Jump to

Keyboard shortcuts

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