database

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package database caches bytes in PostgreSQL or MySQL through a caller-owned Bun DB. Apply the central cache_entries migration before use; constructors never run DDL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Table            string        // default cache_entries; bare lowercase SQL identifier
	Namespace        string        // default default; separates stores sharing a table
	MaxEntryBytes    int           // key + value bytes on Set; default 1 MiB
	OperationTimeout time.Duration // default 5 seconds
}

func (Config) Validate

func (c Config) Validate() error

Validate checks settings without I/O. Zero fields use the documented defaults.

type Store

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

Store borrows DB and never closes it. Cache operations join the transaction in ctx through pkg/database.Executor. Construct once; safe for concurrent use.

func New

func New(db *bun.DB, cfg Config) (*Store, error)

func (*Store) Clear

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

Clear deletes this namespace through the caller's transaction executor. It does not drop tables or close the shared database pool.

func (*Store) Delete

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

func (*Store) Exists

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

func (*Store) Get

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

func (*Store) GetMulti

func (s *Store) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

func (*Store) PruneExpired

func (s *Store) PruneExpired(ctx context.Context, limit int) (int64, error)

PruneExpired deletes at most limit expired rows in this namespace (1–10000). Call periodically outside business transactions. It uses a separate, bounded READ COMMITTED transaction, skips locked rows and never deletes a refreshed row. Expired values are already misses even if this cleanup has not run.

func (*Store) Remember

func (s *Store) Remember(ctx context.Context, key string, ttl time.Duration, load func(context.Context) (any, error)) (any, error)

Remember returns a cached JSON value or loads it on a miss. See cache.Remember for validation, error, expiry and concurrent-loading semantics.

func (*Store) Set

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

func (*Store) SetMulti

func (s *Store) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) error

Jump to

Keyboard shortcuts

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