rkey

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package rkey is a database-backed key repository. It provides methods to interact with keys 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 key repository. A key is a unique identifier for a data structure (string, list, hash, etc.). Use the key repository to manage all keys regardless of their type.

func New

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

New creates a new database-backed key repository. Does not create the database schema.

func (*DB) Count

func (db *DB) Count(keys ...string) (int, error)

Count returns the number of existing keys among specified.

func (*DB) Delete

func (db *DB) Delete(keys ...string) (int, error)

Delete deletes keys and their values, regardless of the type. Returns the number of deleted keys. Non-existing keys are ignored.

func (*DB) DeleteAll

func (db *DB) DeleteAll() error

DeleteAll deletes all keys and their values, effectively resetting the database. Should not be run inside a database transaction.

func (*DB) DeleteExpired

func (db *DB) DeleteExpired(n int) (count int, err error)

DeleteExpired deletes keys with expired TTL, but no more than n keys. If n = 0, deletes all expired keys.

func (*DB) Exists

func (db *DB) Exists(key string) (bool, error)

Exists reports whether the key exists.

func (*DB) Expire

func (db *DB) Expire(key string, ttl time.Duration) error

Expire sets a time-to-live (ttl) for the key using a relative duration. After the ttl passes, the key is expired and no longer exists. If the key does not exist, returns ErrNotFound.

func (*DB) ExpireAt

func (db *DB) ExpireAt(key string, at time.Time) error

ExpireAt sets an expiration time for the key. After this time, the key is expired and no longer exists. If the key does not exist, returns ErrNotFound.

func (*DB) Get

func (db *DB) Get(key string) (core.Key, error)

Get returns a specific key with all associated details. If the key does not exist, returns ErrNotFound.

func (*DB) Keys

func (db *DB) Keys(pattern string) ([]core.Key, error)

Keys returns all keys matching pattern. Supports glob-style patterns like these:

key*  k?y  k[bce]y  k[!a-c][y-z]

Use this method only if you are sure that the number of keys is limited. Otherwise, use the DB.Scan or DB.Scanner methods.

func (*DB) Len

func (db *DB) Len() (int, error)

Len returns the total number of keys, including expired ones.

func (*DB) Persist

func (db *DB) Persist(key string) error

Persist removes the expiration time for the key. If the key does not exist, returns ErrNotFound.

func (*DB) Random

func (db *DB) Random() (core.Key, error)

Random returns a random key. If there are no keys, returns ErrNotFound.

func (*DB) Rename

func (db *DB) Rename(key, newKey string) error

Rename changes the key name. If there is an existing key with the new name, it is replaced. If the old key does not exist, returns ErrNotFound.

func (*DB) RenameNotExists

func (db *DB) RenameNotExists(key, newKey string) (bool, error)

RenameNotExists changes the key name. If there is an existing key with the new name, does nothing. Returns true if the key was renamed, false otherwise.

func (*DB) Scan

func (db *DB) Scan(cursor int, pattern string, ktype core.TypeID, count int) (ScanResult, error)

Scan iterates over keys matching pattern. Returns a slice of keys (see core.Key) of size count based on the current state of the cursor. Returns an empty slice when there are no more keys.

Filtering and limiting options:

  • pattern (glob-style) to filter keys by name (* = any name).
  • ktype to filter keys by type (TypeAny = any type).
  • count to limit the number of keys returned (0 = default).

func (*DB) Scanner

func (db *DB) Scanner(pattern string, ktype core.TypeID, pageSize int) *Scanner

Scanner returns an iterator for keys matching pattern. The scanner returns keys one by one, fetching them from the database in pageSize batches when necessary. Stops when there are no more items or an error occurs.

Filtering and pagination options:

  • pattern (glob-style) to filter keys by name (* = any name).
  • ktype to filter keys by type (TypeAny = any type).
  • pageSize to limit the number of keys fetched at once (0 = default).

type ScanResult

type ScanResult struct {
	Cursor int
	Keys   []core.Key
}

ScanResult represents a result of the Scan call.

type Scanner

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

Scanner is the iterator for keys. Stops when there are no more keys or an error occurs.

func (*Scanner) Err

func (sc *Scanner) Err() error

Err returns the first error encountered during iteration.

func (*Scanner) Key

func (sc *Scanner) Key() core.Key

Key returns the current key.

func (*Scanner) Scan

func (sc *Scanner) Scan() bool

Scan advances to the next key, fetching keys from db as necessary. Returns false when there are no more keys or an error occurs.

type Tx

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

Tx is a key repository transaction.

func NewTx

func NewTx(tx sqlx.Tx) *Tx

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

func (*Tx) Count

func (tx *Tx) Count(keys ...string) (int, error)

Count returns the number of existing keys among specified.

func (*Tx) Delete

func (tx *Tx) Delete(keys ...string) (int, error)

Delete deletes keys and their values, regardless of the type. Returns the number of deleted keys. Non-existing keys are ignored.

func (*Tx) DeleteAll

func (tx *Tx) DeleteAll() error

DeleteAll deletes all keys and their values, effectively resetting the database. Should not be run inside a database transaction.

func (*Tx) Exists

func (tx *Tx) Exists(key string) (bool, error)

Exists reports whether the key exists.

func (*Tx) Expire

func (tx *Tx) Expire(key string, ttl time.Duration) error

Expire sets a time-to-live (ttl) for the key using a relative duration. After the ttl passes, the key is expired and no longer exists. If the key does not exist, returns ErrNotFound.

func (*Tx) ExpireAt

func (tx *Tx) ExpireAt(key string, at time.Time) error

ExpireAt sets an expiration time for the key. After this time, the key is expired and no longer exists. If the key does not exist, returns ErrNotFound.

func (*Tx) Get

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

Get returns a specific key with all associated details. If the key does not exist, returns ErrNotFound.

func (*Tx) Keys

func (tx *Tx) Keys(pattern string) ([]core.Key, error)

Keys returns all keys matching pattern. Supports glob-style patterns like these:

key*  k?y  k[bce]y  k[!a-c][y-z]

Use this method only if you are sure that the number of keys is limited. Otherwise, use the Tx.Scan or Tx.Scanner methods.

func (*Tx) Len

func (tx *Tx) Len() (int, error)

Len returns the total number of keys, including expired ones.

func (*Tx) Persist

func (tx *Tx) Persist(key string) error

Persist removes the expiration time for the key. If the key does not exist, returns ErrNotFound.

func (*Tx) Random

func (tx *Tx) Random() (core.Key, error)

Random returns a random key. If there are no keys, returns ErrNotFound.

func (*Tx) Rename

func (tx *Tx) Rename(key, newKey string) error

Rename changes the key name. If there is an existing key with the new name, it is replaced. If the old key does not exist, returns ErrNotFound.

func (*Tx) RenameNotExists

func (tx *Tx) RenameNotExists(key, newKey string) (bool, error)

RenameNotExists changes the key name. If there is an existing key with the new name, does nothing. Returns true if the key was renamed, false otherwise.

func (*Tx) Scan

func (tx *Tx) Scan(cursor int, pattern string, ktype core.TypeID, count int) (ScanResult, error)

Scan iterates over keys matching pattern. Returns a slice of keys (see core.Key) of size count based on the current state of the cursor. Returns an empty slice when there are no more keys.

Filtering and limiting options:

  • pattern (glob-style) to filter keys by name (* = any name).
  • ktype to filter keys by type (TypeAny = any type).
  • count to limit the number of keys returned (0 = default).

func (*Tx) Scanner

func (tx *Tx) Scanner(pattern string, ktype core.TypeID, pageSize int) *Scanner

Scanner returns an iterator for keys matching pattern. The scanner returns keys one by one, fetching them from the database in pageSize batches when necessary. Stops when there are no more items or an error occurs.

Filtering and pagination options:

  • pattern (glob-style) to filter keys by name (* = any name).
  • ktype to filter keys by type (TypeAny = any type).
  • pageSize to limit the number of keys fetched at once (0 = default).

Jump to

Keyboard shortcuts

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