postgres

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 14 Imported by: ? 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 postgres provides a PostgreSQL-backed cache store.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilPool  = errors.New("cache postgres: pool must not be nil")
	ErrNilCodec = errors.New("cache postgres: codec must not be nil")
)
View Source
var ErrInvalidationPayloadTooLarge = errors.New("cache postgres: invalidation payload exceeds PostgreSQL notification limit")

ErrInvalidationPayloadTooLarge reports an invalidation event whose encoded payload does not fit in a PostgreSQL NOTIFY payload (8000 bytes).

Functions

This section is empty.

Types

type InvalidationBus

type InvalidationBus[K comparable] struct {
	// contains filtered or unexported fields
}

InvalidationBus distributes invalidations through PostgreSQL LISTEN/NOTIFY.

func NewInvalidationBus

func NewInvalidationBus[K comparable](pool *pgxpool.Pool, options ...InvalidationOption[K]) (*InvalidationBus[K], error)

NewInvalidationBus constructs a PostgreSQL invalidation bus.

func (*InvalidationBus[K]) Publish

func (b *InvalidationBus[K]) Publish(ctx context.Context, event cache.InvalidationEvent[K]) error

Publish sends event to the subscribers of event.Namespace.

func (*InvalidationBus[K]) Subscribe

func (b *InvalidationBus[K]) Subscribe(ctx context.Context, namespace string, handler cache.InvalidationHandler[K]) error

Subscribe calls handler for every event published for namespace until ctx ends, returning ctx.Err(), or until receiving or handling an event fails, returning that error. A nil handler is rejected with cache.ErrNilInvalidationHandler.

type InvalidationOption

type InvalidationOption[K comparable] func(*invalidationConfig[K]) error

InvalidationOption configures PostgreSQL invalidation serialization.

func WithInvalidationKeyCodec

func WithInvalidationKeyCodec[K comparable](codec cache.Codec[K]) InvalidationOption[K]

WithInvalidationKeyCodec overrides automatic invalidation-key serialization.

type Locker

type Locker[K comparable] struct {
	// contains filtered or unexported fields
}

Locker provides renewable PostgreSQL row leases.

func NewLocker

func NewLocker[K comparable](pool *pgxpool.Pool, options ...LockerOption[K]) (*Locker[K], error)

NewLocker constructs a PostgreSQL locker using a caller-owned pool.

func (*Locker[K]) Lock

func (l *Locker[K]) Lock(ctx context.Context, namespace string, key K, ttl time.Duration) (cache.Lease, error)

Lock acquires a lease on key in namespace for ttl. It returns cache.ErrLockNotAcquired while another lease is live and cache.ErrInvalidTTL for a non-positive ttl.

type LockerOption

type LockerOption[K comparable] func(*lockerConfig[K]) error

LockerOption configures PostgreSQL lock key serialization.

func WithLockerKeyCodec

func WithLockerKeyCodec[K comparable](codec cache.Codec[K]) LockerOption[K]

WithLockerKeyCodec overrides automatic lock key serialization.

type Option

type Option[K comparable, V any] func(*config[K, V]) error

Option configures PostgreSQL serialization.

func WithKeyCodec

func WithKeyCodec[K comparable, V any](codec cache.Codec[K]) Option[K, V]

WithKeyCodec overrides automatic key serialization.

func WithValueCodec

func WithValueCodec[K comparable, V any](codec cache.Codec[V]) Option[K, V]

WithValueCodec overrides automatic value serialization.

type Store

type Store[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Store persists entries in PostgreSQL. The caller retains ownership of the pool.

func New

func New[K comparable, V any](pool *pgxpool.Pool, options ...Option[K, V]) (*Store[K, V], error)

New constructs a PostgreSQL store using a caller-owned pool.

func (*Store[K, V]) DeleteExpired

func (s *Store[K, V]) DeleteExpired(ctx context.Context) (int64, error)

DeleteExpired removes physically retained entries and locks after logical TTL expiry.

func (*Store[K, V]) Get

func (s *Store[K, V]) Get(ctx context.Context, namespace string, key K) (cache.Entry[V], bool, error)

Get returns the live entry stored under key in namespace and whether one is present. Expired entries and entries written before the namespace's last InvalidateAll are reported as absent.

func (*Store[K, V]) Invalidate

func (s *Store[K, V]) Invalidate(ctx context.Context, namespace string, key K) error

Invalidate removes key from namespace. Removing a missing key is not an error.

func (*Store[K, V]) InvalidateAll

func (s *Store[K, V]) InvalidateAll(ctx context.Context, namespace string) (returnErr error)

InvalidateAll hides every entry in namespace by advancing the namespace generation. Older records are ignored on read and left to expire or be overwritten.

func (*Store[K, V]) Migrate

func (s *Store[K, V]) Migrate(ctx context.Context) error

Migrate creates the idempotent cache tables and expiration index.

func (*Store[K, V]) Set

func (s *Store[K, V]) Set(ctx context.Context, namespace string, key K, entry cache.Entry[V]) (returnErr error)

Set stores entry under key in namespace, replacing any previous entry. An entry with a zero ExpiresAt never expires.

Jump to

Keyboard shortcuts

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