sqlkv

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlkv implements kv.Cache on a single table of the SQL database you already run for theauth storage. It works on Postgres, MySQL and SQLite.

Build the full set of shared stores with kv.FromCache(store). Create the table once with EnsureSchema (idempotent) or run Schema(dialect) in your own migration tool.

Index

Constants

View Source
const DefaultPrefix = "theauth:"

DefaultPrefix is prepended to every key unless WithPrefix or WithoutPrefix says otherwise. Prune only removes rows under this store's prefix.

View Source
const DefaultTable = "theauth_kv"

DefaultTable is the table name used when none is given.

Variables

This section is empty.

Functions

func Schema

func Schema(d Dialect, table string) string

Schema returns the DDL for the given dialect and table.

Types

type Dialect

type Dialect int

Dialect selects the SQL flavor.

const (
	Postgres Dialect = iota
	MySQL
	SQLite
)

Supported dialects.

type Option

type Option func(*Store)

Option tunes New.

func WithClock

func WithClock(now func() time.Time) Option

WithClock injects the time source (tests).

func WithPrefix added in v2.12.0

func WithPrefix(prefix string) Option

WithPrefix sets the prefix prepended to every stored key. WithPrefix("") turns prefixing off and keeps the exact key names the library chose, which is how a deployment keeps the rows it wrote before the default prefix existed.

func WithTable

func WithTable(name string) Option

WithTable overrides the table name. The name is validated as a plain identifier because it is interpolated into SQL.

func WithoutPrefix added in v2.12.0

func WithoutPrefix() Option

WithoutPrefix disables the prefix. It is shorthand for WithPrefix("").

type Store

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

Store implements kv.Cache over database/sql.

func New

func New(db *sql.DB, dialect Dialect, opts ...Option) (*Store, error)

New returns a Store whose keys carry DefaultPrefix. It does not touch the database; call EnsureSchema.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, key string) error

Delete implements kv.Cache.

func (*Store) EnsureSchema

func (s *Store) EnsureSchema(ctx context.Context) error

EnsureSchema creates the table and its expiry index when missing.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) ([]byte, bool, error)

Get implements kv.Cache.

func (*Store) Incr

func (s *Store) Incr(ctx context.Context, key string, ttl time.Duration) (int64, error)

Incr implements kv.Cache.

func (*Store) Prune

func (s *Store) Prune(ctx context.Context) (int64, error)

Prune deletes expired rows under this store's prefix and returns how many it removed. Rows under other prefixes, including other apps sharing the table, are never touched. Writes call it opportunistically; call it from a timer if the table sees little traffic.

func (*Store) Set

func (s *Store) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set implements kv.Cache.

func (*Store) SetNX

func (s *Store) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error)

SetNX implements kv.Cache.

Jump to

Keyboard shortcuts

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